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
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.
OnlineAnnouncementReceived and OfflineAnnouncementReceived events implementation.
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.
Once service goes to offline client will receive the announcement and OfflineAnnouncementReceived will be executed.
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:
appvn
appvn apk
appvn app
appvn download
Outstanding article! I want people to know just how good this information is in your article.
Post a Comment