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:
The behavior can also be configured through the configuration file:
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.
When an offline/online announcement is received, we have access to the endpoint discovery metadata through the AnnouncementEventArgs:
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.