Discovery Announcements
Now that you are familiar with some Discovery Concepts, let’s find out more about Announcements. I will use the terms:
- Target Service: an endpoint that makes itself available for discovery,
- Client: an endpoint that searches for Target Services,
- Discovery Proxy: an endpoint that facilitates discovery of Target Services by Clients.
When a client needs to find a service, one way to search for it is to poll for available services. Another way is to listen for the Hello and Bye announcements. With this approach, the network traffic is reduced and the Client can learn about the presence/departure of the service as soon as announcements are received.
When a service joins a network and becomes discoverable, it sends a multicast Hello message that contains key information about the target service, announcing its availability to listening clients. Similarly, when a Target Service leaves a network, it sends a Bye message announcing its departure. This defines the ad-hoc mode of operation.
To scale to a large number of endpoints, the Discovery protocol defines a managed mode of operation, when a Discovery Proxy is used to find Target Services. In this case, the target service sends unicast announcement messages to a Discovery Proxy. When a Discovery Proxy detects a probe or resolution request sent multicast on an ad hoc network, it sends an announcement for itself, letting the Clients know of its existence.
WCF Discovery offers the AnnouncementEndpoint and UdpAnnouncementEndpoint standard endpoints to allow services and clients to easily send Hello and Bye announcements. Here are the simple steps to configure them to use announcements:
On the service side:
To configure the service to send announcements, we need to add a ServiceDiscoveryBehavior with an announcement endpoint. Considering the CalculatorService we talked about in previous samples and a ServiceHost called serviceHost that hosts it, we can programmatically add the behavior like this:
ServiceDiscoveryBehavior serviceDiscoveryBehavior = new ServiceDiscoveryBehavior();
// Announce the availability of the service over UDP multicast
serviceDiscoveryBehavior.AnnouncementEndpoints.Add( newUdpAnnouncementEndpoint ());
// Make the service discoverable over UDP multicast
serviceHost.Description.Behaviors.Add(serviceDiscoveryBehavior);
The behavior can also be configured through the configuration file:
<services>
<service behaviorConfiguration="CalculatorBehavior" name="Microsoft.Samples.Discovery.CalculatorService">
<!--Add Discovery Endpoint-->
<endpoint name="udpDiscoveryEpt" kind="udpDiscoveryEndpoint" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorBehavior">
<!--Add Discovery behavior-->
<serviceDiscovery>
< announcementEndpoints >
< endpointkind = " udpAnnouncementEndpoint " />
</ announcementEndpoints >
</serviceDiscovery>
</behavior>
</serviceBehaviors>
</behaviors>
With this simple change, services will send announcements when starting and closing. If the services are aborted, the messages are not sent.
On the client side:
We need to host an AnnouncementService and subscribe to the OnlineAnnouncementReceived and OfflineAnnouncementReceived events.
// Create an AnnouncementService instance
AnnouncementService announcementService = new AnnouncementService();
// Subscribe the announcement events
announcementService.OnlineAnnouncementReceived += OnOnlineEvent;
announcementService.OfflineAnnouncementReceived += OnOfflineEvent;
// Create ServiceHost for the AnnouncementService
using (ServiceHost announcementServiceHost = new ServiceHost(announcementService))
{
// Listen for the announcements sent over UDP multicast
announcementServiceHost.AddServiceEndpoint(new UdpAnnouncementEndpoint());
announcementServiceHost.Open();
Console.WriteLine("Press <ENTER> to terminate.");
Console.ReadLine();
}
When an offline/online announcement is received, we have access to the endpoint discovery metadata through the AnnouncementEventArgs:
static void OnOnlineEvent(object sender, AnnouncementEventArgs e)
{
Console.WriteLine("Received an online announcement from {0}", e.EndpointDiscoveryMetadata.Address);
}
static void OnOfflineEvent(object sender, AnnouncementEventArgs e)
{
Console.WriteLine("Received an offline announcement from {0}", e.EndpointDiscoveryMetadata.Address);
}
You can check out the working code in the Announcements .NET Beta1 discovery sample. Beyond this simple usage, the user can extend the announcement model (eg. custom announcements). WCF Discovery also offers the possibility to send announcements explicitly, using AnnouncementClient. Stay tuned for more about this in a future post.
Comments
- Anonymous
September 13, 2010
how could DiscoveryProxy configured through the configuration file ?