Friday, August 14, 2009

WS-Discovery Protocol in WCF 4.0 –Part 11 –[Announcement]

In First part of this series  Client is looking for service based on criteria such as Service type. In this post we are going to use Announcement. In this case Service will Announce themselves to the network and Clients can be configured to listen for service announcements.   

In the default case a service sends out a hello message when it comes online and a bye message when going offline. This pattern is useful when a service wants to notify a recipient without having the recipient pinging the network periodically.

On the Service side we need to add ServiceDiscovery Behavior as shown below

image  

Client side we need to Host AnnouncementService and subscribe for OnlineAnnouncementReceived and OfflineAnnouncementReceived events.

Here is the client code to host AnnouncementService   and subscribing the events.

image

OnlineAnnouncementReceived and OfflineAnnouncementReceived events implementation.

image

Now to test first Start the client application  after that Start the Service application .

when service comes to online OnlineAnnouncementReceived event handler will be executed.

image

Once service goes to offline client will receive the announcement and  OfflineAnnouncementReceived  will be executed.

image

Note: if service is aborted  then the client won’t receive Announcement message . ( This is why i close the Service host when any key is pressed and display the message saying that service is offline now)

My Service code snippet

namespace SvcHost {
    class Program {
        static void Main(string[] args)
        {
            ServiceHost svc = new ServiceHost(typeof(Service1),new Uri("http://localhost:1394/Service1.svc"));
            svc.Open();
            Console.WriteLine("Service is online Now.[Press any key to close the service.]");
            Console.ReadLine();
            svc.Close();
            Console.WriteLine("Service is offline Now");
            Console.ReadLine();

        }
    }
}

This pattern is useful when a service wants to notify a recipient without having the recipient pinging the network periodically.

Thanks(Nandri)

SreenivasaRagavan

1 comment:

alekhya sweetie said...

appvn
appvn apk
appvn app
appvn download
Outstanding article! I want people to know just how good this information is in your article.