Service Discovery Techniques Introduced in WCF 4.0

Service Discovery

In WCF or the .NET
Framework web services bonding between the client and the service is actually
created only when the services are discovered and their proxy reference is
added to the client. If the URL of the hosted service is a fixed one, i.e., if
it doesn’t change over time the service can be discovered by “Add Web
Reference” – for Web Services or “Add Service Reference” – for WCF. But for
instance what if the service is designed to change its host URL quite
frequently? Or what if the service would come on or off the network in a
scheduled manner?

WCF 4.0 introduces a couple of
service discovery techniques, which the client can use to discover the services
dynamically through client probing or service announcement. WCF 4.0 comes up
with the ability of dynamic discovery. This feature allows the client to probe
for the services or reversely the services can also announce their presence on
the network. Below are the dynamic service discovery techniques used in WCF

  • Ad-Hoc
    or UDP Discovery
  • Managed

Ad-Hoc Discovery

Ad-Hoc discovery follows a
broadcast model by sending multi cast request probe messages over the network
and the matching services respond back with a probe match response to the
client. The probe match response would have the endpoint details about the

In order to make a service discoverable,
the .net framework 4.0 provides the endpoint class named UdpDiscoveryEndpoint
lying under the System.ServiceModel.Discovery namespace. Also the service
behavior named ServiceDiscoveryBehavior should be added to the service. From
the client perspective, services can be discovered using the class
DiscoveryClient. This class is responsible for issuing multi cast messages
using UDP protocol.
Services can be searched by providing the find criteria such as MaxResults to
be returned, contract type, scope, etc. Only the matching services would send
back a probe match response to the client.

Dynamic service discovery can be
implemented on both the client and WCF service using the configuration file or
through code behind. The config file option provides the capability of converting
the existing WCF services to be dynamically discovered. Below is the sample
code demonstrating the ad-hoc WCF service discovery.

Service Host:

namespace AdhocServiceHost
    class Program
        static void Main(string[] args)
            Uri hostAddress = new Uri("http://localhost:8888/GreetingService");
            using (ServiceHost serviceHost = new ServiceHost(typeof(GreetingService.Implementation.GreetingService), hostAddress))
                //Add a greeting endpoint
                serviceHost.AddServiceEndpoint(typeof(IGreetingService), new WSHttpBinding(), "http://localhost:8888/GreetingService/Greeting");
                ServiceMetadataBehavior metaDataBehavior = new ServiceMetadataBehavior();
                metaDataBehavior.HttpGetEnabled = true;
                //Add the ServiceDiscoveryBehavior
                serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
                //Add the discovery endpoint
                serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
                //Open the host
                Console.WriteLine("Greeting Service Hosting URL: {0}", hostAddress);
                Console.WriteLine("Press any key to close the service");

Client code:

namespace AdhocServiceClient
    class Program
        static void Main(string[] args)
            DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
            //Set the probe criteria
            FindCriteria findCriteria = new FindCriteria(typeof(IGreetingService));
            //Only the first result will be returned.
            findCriteria.MaxResults = 1;
            Collection<EndpointDiscoveryMetadata> endpointCollection = discoveryClient.Find(findCriteria).Endpoints;
            if (endpointCollection.Count > 0)
                GreetingServiceClient client = new GreetingServiceClient(new WSHttpBinding(), endpointCollection[0].Address);
                Console.WriteLine("No matching services discovered!");

This discovery technique is very
useful when the service and client are lying in the same subnet. UDP (User
Datagram Protocol) is the one that supports sending a multicast message to all
on a local network.

Additionally, the WCF 4.0 service
discovery feature allows the WCF service to announce its presence over the

Managed Discovery

Unlike the ad-hoc discovery,
managed discovery can be used to probe for services over a broader network.
When I say broader network it includes the internet as well. In the managed
discovery model, apart from the client and the service, there is an
intermediate element called the DiscoveryProxyService.

It works like this, the service
will make the announcement of its current state (online or offline) to the
DiscoveryProxyService, which is hosted on a separate server and the
DiscoveryProxyService takes care of caching the endpoint details in it.
Developers can create their own DicoveryProxyService by deriving their class
from the in-build DiscoveryProxy class.

Clients will send the unicast
probe message directly to the DiscoveryProxyService and the later takes care of
providing the probe match response back to the client based on the service
endpoint information that it has cached.

Hope this article was
informative. Happy reading!

More by Author

Must Read