Discovery Proxy Sample
This sample shows how to create an implementation of a Discovery proxy to store information about existing services and how clients can query that proxy for information. This sample consists of three projects:
Service: A simple Windows Communication Foundation (WCF) calculator service that registers itself with the discovery proxy.
Discovery Proxy: The implementation of a discovery proxy service.
Client: A WCF client application that calls the discovery proxy to search for services.
Discovery proxy implementation
The samples may already be installed on your machine. Check for the following (default) directory before continuing.
Main method of the Program.cs file, the sample shows how a service of type DiscoveryProxy is hosted. It exposes two endpoints, one of type DiscoveryEndpoint and another of type AnnouncementEndpoint. Both of the endpoints use TCP as a transport. The DiscoveryEndpoint is listening at the URI specified by the
probeEndpointAddress parameter, this is where clients can send probe messages to query the proxy for its data. The AnnouncementEndpoint is listening at the URI specified by the
announcementEndpointAddress parameter. This is where the proxy listens to for announcements. When an online announcement is received, the proxy adds the service to its cache and when an offline announcement is received it removes the service from its cache.
The DiscoveryProxy.cs contains the implementation of the DiscoveryProxy. The Proxy must inherit from the DiscoveryProxyBase class and requires an implementation of AsyncResult. At instantiation, the Proxy creates a new Dictionary, which it uses to store elements it knows about.
The file is divided into two regions, Proxy Cache Methods and Discovery Proxy Implementation. The Proxy Cache Methods region contains methods used to update the Dictionary, perform queries against the Dictionary, and print the data for users. The Discovery Proxy Implementation region contains the overridden methods required for the Announcement and Probe functionality. They define the actions taken by a proxy after receiving an Online Announcement, an Offline Announcement, or a Probe message.
In the Program.cs file in the Service project, the same URI is used for its announcement endpoint as the discovery proxy. This is because service uses the endpoint for sending the announcements, while the proxy uses it for receiving them. The service uses the DiscoveryBehavior and adds an announcement endpoint to it.
The Client project uses the same URI for its probe endpoint as the Proxy. This is because the probes in this scenario are also being unicast specifically to the endpoint available on the proxy. The Client connects to this well-known address and then queries it for the service. Once it has found the service it connects to it.
To use this sample
Load the project solution in Visual Studio 2010 and build the project.
First run the Discovery Proxy application, generated in [solution base directory]\DiscoveryProxy\bin\debug. The Discovery Proxy must run first because TCP announcement endpoints must be up for the service to send its announcements.
Second, run the service application generated in [solution base directory]\Service\bin\debug. At start-up, the service sends an announcement to the announcement endpoint of the discovery proxy and is registered in the proxy’s cache.
Next, run the client application, generated in [solution base directory]\Client\bin\debug. The client queries the proxy, gets the service address and then connects to the service.
Lastly, terminate the client, service and then the proxy. The proxy must be running for it to receive the service's offline announcement.