Tuesday, September 25, 2012

Intimate client when Server is down in WCF using AnnouncementEndoint



In client server application when server stops or goes offline , It is important that client is intimated about it immediately, So that client can act on that.
WCF 4 provides feature of announcement using AnnouncementEndoint, by which server can announce their availability like coming up and going down (Hello and Bye messages). Client on the other hand can listen for these announcement and act on them.

AnnouncementEndoint configuration in Server

On server side we can configure to send the announcement by using following configuration in app.config 
  

As highlighted above config of server part should contain endpoint with name udpDiscoveryEpt whose kind is udpDiscoveryEndpoint ; This is a standard endpoint provided by the WCF. Note that this is UDP endpoint so that it will multicast announcement. above figure also highlight the announcement behaviors.

At Server side we need to do only this much no code nothing and server will start multicasting announcement when it comes up and goes down.

Configuration in Client

When writing a client application for this server , first step is add reference to this server it will generate proxy and all config entries automatically. no special configuration needs to be done at client. Following code needs to be added in client for receiving these announcement.

  
WCF 4 has provided new class AnnoucementService this class is present in System.ServiceModel.Discovery namespace for that you need to first add reference to the .NET dll System.ServiceModel.Discovery. We first create object of class AnnoucementService and then attach handler for event OfflineAnnouncementReceived, This event shall be called when client receives udp packet saying that server is down. After this we host this AnnoucementService using service host.

Note from MSDN

"Announcements are sent when the service host opens and closes. If these calls do not finish properly the announcement message may not be sent out."

You can find the code for this article here

4 comments:

Anonymous said...

The recommendations you discussed here are really useful. It absolutely was such a fun surprise to have that awaiting me after i woke up to find this event. Thank you very much for the tip!

Anonymous said...

This is a good blog message, I will keep the post in my mind. If you can add more video and pictures can be much better. Because they help much clear understanding. :) thanks Cavalieri.

Sanket Kulkarni said...

Thanks Cavalieri for your comments, I will try to add video and pictures in my blog messages henceforth

Anonymous said...
This comment has been removed by a blog administrator.