Locating a Service Ad Hoc
service consumer can send out WS-Discovery probe messages to locate
available services. The probe query can include compatibility criteria,
such as a service contract or a service scope. Query parameters are
encoded in a FindCriteria object.
There are two types of query criteria:
contract type names – searches for services with endpoints that implement the specified contract names
– searches for services with endpoints that match the specified scope
(scopes are defined in the endpoint’s behavior and several matching
options for complete or partial matches exist)
The DiscoveryClient class provided by WCF manages probes and raises FindProgressChangedEventArgs events when ProbeMatch responses come in:
new DiscoveryClient(new UdpDiscoveryEndpoint());
Following this, we are submitting an asynchronous discovery query over UDP. ParseResult examines the service contract, scope, and metadata to determine if the responding service meets the requirements:
private void OnFindProgressChanged(
object sender, FindProgressChangedEventArgs e)
The DiscoveryClient class also implements a synchronous Find() method that takes a FindCriteria
parameter that specifies query and query completion criteria, such as
the number of responses or the time to wait for responses.
DiscoveryClient also exposes a Resolve() method to locate a replacement for a service that’s been previously available at a known address. Calling Resolve() follows the same pattern as Find().
Sending and Receiving Service Announcements
Services can announce
their availability when they come online or go offline. These
announcements can be received by all services listening for them. You
configure a service to transmit announcements by adding announcementEndpoints to the serviceDiscovery behavior, as shown in Example 6.20. The service then issues announcements to the configured endpoint.
The standard udpAnnouncementEndpoint
is preconfigured in WCF. Each announcement includes the service
endpoint location and contract, as well as endpoint-specific metadata.
In highly dynamic
environments, service consumers may want to track available services
instead of probing for availability or relying on the discovery proxy.
Probing introduces additional latency and therefore should not occur as
part of the logic that resolves service
locations. Frequent multicast probing further results in unnecessary
network traffic. Instead, a consumer of dynamically available services
can listen for announcement broadcasts and maintain its own list of
Services interested in receiving local UDP discovery announcements must open up a listener on the udpAnnouncementEndpoint endpoint. WCF provides a pre-built AnnouncementService class to handle service announcements. This class can raise the OnOnlineAnnouncement and OnOfflineAnnouncement events to the hosting application.
This example shows a listener configured for announcement services:
... application service information ...
<endpoint kind="udpAnnouncementEndpoint" />
Next, we register events with AnnouncementService:
AnnouncementService announcementService =
The events receive an EndpointDiscoveryMetadata object, just like the response to a discovery probe. A FindCriteria object determines if the metadata matches endpoint requirements. In the following example, we query an EndpointDiscoveryMetadata announcement for compatibility with the ICustomerService contract:
private void OnlineAnnouncement
(object sender, AnnouncementEventArgs e)
EndpointDiscoveryMetadata metadata =
FindCriteria criteria =
... further examine endpoint metadata ...
... store endpoint address for service access ...