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> discoveryClient_FindCompleted);
discClient.FindProgressChanged += new EventHandler<FindProgressChangedEventArgs>(discoveryClient_FindProgressChanged);
discClient.FindAsync(new FindCriteria(typeof(IService1))); Console.ReadLine(); } 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