WCF and Service Discovery in .NET 4.0 (Discovery Part II)

Damir Dobric Posts

Next talks:



Follow me on Twitter: #ddobric




This post describe how to implement the client able to perform discovery the service based on WS-Discovery protocol.
If you are interested on discovery details this post describes some important topics of WS-discovery protocol specification.

The client (or thread) performing discovery should make a usage of the class DiscoveryClient, which is currently implemented in the namespace System.ServiceModel.Discovery and assembly System.WorkflowServiceModel.Dll.
To discover the service the client has to create FindCriteria. This criteria simply contains the interface of the target service:

FindCriteria criteria new FindCriteria(typeof(IService1)

After the criteria is defined the discovery process can be started either synchronous or asynchronously by using of Find or FindAsync methods respective. Practically you will probable mostly use the FindAsync (see example below).

During discovery you can register several events handlers. For example FindProgressChanged is invoked each time one of service endpoints has been discovered. This is interesting when the clients is looking for any endpoint.
If you want to be notified when the discovery process is finished then the handler FindCompleted would be the better solution. This handler is invoked at the end of discovery when all endpoints have been discovered.

Following example shows how to implement the client which asynchronously discovers the service IService1:

  static void Main(string[] args)
    DiscoveryClient discClient =
    new DiscoveryClient(new UdpDiscoveryEndpoint());
    discClient.FindCompleted += new EventHandler<FindCompletedEventArgs>

    discClient.FindProgressChanged +=
    new EventHandler<FindProgressChangedEventArgs>(discoveryClient_FindProgressChanged);

    discClient.FindAsync(new FindCriteria(typeof(IService1)));



   static void discoveryClient_FindProgressChanged(object sender,
                                                   FindProgressChangedEventArgs e)
   { }

   static void discoveryClient_FindCompleted(object sender, FindCompletedEventArgs e)
   {  }


Next picture shows the result of discovery process. The result shown at the picture is the value of the parameter FindCompletedEventArgs of the event handler (discoveryClient_FindCompleted) DiscoveryClient.FindCompleted:


The picture shows that the target service has been discovered at two endpoints. The second one shows that the service supports
listening based at wsHttpBinding.

Related Posts:

1. Part I: WCF and Service Discovery in .NET 4.0
2. Part III: NET 4.0 : Enabling WCF service for discovery


Visit www.daenet.de

Posted Nov 23 2008, 08:54 PM by Damir Dobric


Jean-Michel Bezeau wrote re: WCF and Service Discovery in .NET 4.0 (Discovery Part II)
on 03-31-2011 20:08

I would like to see the same sample without any change in an existing client. I wish to implement the Find into the config file. I am looking for a solution where I limit change to both WCF Services and WCF Clients code. Isn't that the purpose of WCF in the first place?

Mitch wrote re: WCF and Service Discovery in .NET 4.0 (Discovery Part II)
on 10-03-2011 22:15

You're the one with the brains here. I'm wtcanhig for your posts.

developers.de is a .Net Community Blog powered by daenet GmbH.