WCF Discovery in .NET 4.0

Introduction

 
Just imagine that your service has only one instance running and for some reason your service goes down. So the first question is how to handle this situation. In .Net 4.0 and above, Windows Communication Foundation has introduced a new feature of WCF; Discovery and with the help of this feature we can access the other service.
 

WCF Discovery

 
Windows Communication Foundation (WCF) provides support to enable services to be discoverable at runtime in an interoperable way using the WS-Discovery protocol. WCF services can announce their availability to the network using a multicast message or to discover a proxy server. Client applications can search the network or a discovery proxy server to find services that meet a set of criteria. The topics in this section provide an overview and describe the programming model for this feature in detail.
 
Reference: WCF Discovery
 
Step 1: First of all we will create a new project in Visual Studio 2010 and 2011. The project type should WCF Service Library. I am using a sample default WCF template.
  1. [ServiceContract]  
  2. public interface IService1  
  3. {  
  4.     [OperationContract]  
  5.     string GetData(int value);  
  6.     [OperationContract]  
  7.     CompositeType GetDataUsingDataContract(CompositeType composite);  
  8. }  
Step 2: Now we will add new projects which are Console Applications; we will use this project to host our WCF service. We will add two Console Applications for the host WCF service. Actually we need two running instances of the Service.
  1. static void Main(string[] args)  
  2. {  
  3.      Uri baseAddress = new Uri(string.Format("http://{0}:8000/discovery/scenarios/Myservice/{1}/",  
  4.      System.Net.Dns.GetHostName(), Guid.NewGuid().ToString()));  
  5.      Console.WriteLine(baseAddress);  
  6.      using (ServiceHost serviceHost = new ServiceHost(typeof(SampleDicoveryService.Service1), baseAddress))  
  7.      {  
  8.           serviceHost.AddServiceEndpoint(typeof(SampleDicoveryService.IService1), new WSHttpBinding(), string.Empty);  
  9.           serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());  
  10.           serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());  
  11.           serviceHost.Open();  
  12.           Console.WriteLine("Press to terminate service.");  
  13.           Console.ReadLine();  
  14.      }  
  15. }  
Step 3: Now we will add a new project in the same solution. This will be the client application for our WCF service. Now you see the following highlighted text. We have to add only UDPDiscoveryEndPoint while hosting the service.
  1. static EndpointAddress serviceAddress;  
  2. static void Main()  
  3. {  
  4.     if (FindService()) InvokeService();  
  5. }  
  6. static bool FindService()  
  7. {  
  8.     Console.WriteLine("\nFinding Myservice Service ..");  
  9.     DiscoveryClient discoveryClient =  
  10.           new DiscoveryClient(new UdpDiscoveryEndpoint());  
  11.     var Services =  
  12.          discoveryClient.Find(new FindCriteria(typeof(ServiceReference1.IService1)));  
  13.     discoveryClient.Close();  
  14.     if (Services == null)  
  15.     {  
  16.          Console.WriteLine("\nNo services are found.");  
  17.          return false;  
  18.     }  
  19.     else  
  20.     {  
  21.          serviceAddress = Services.Endpoints[0].Address;  
  22.          return true;  
  23.     }  
  24. }  
  25. static void InvokeService()  
  26. {  
  27.      Console.WriteLine("\nInvoking My Service at {0}\n", serviceAddress);  
  28.      ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();  
  29.      client.Endpoint.Address = serviceAddress;  
  30.      client.GetData(1);  
  31. }  
Step 4: Now with the help of Set Startup Project Wizard we will start multiple projects:
 
WCFdiscovery_Seting.png
 
WCFdiscovery_multi.png 
 
Step 5: Now we will see the service host output:
 
WCFdiscoveryHost.png 
 
In the following image we can see how our service is running:
 
WcfDiscovery_Browser.png 
 
Step 6: Now finally will we run our client application and will see how many endpoints are running:
 
WCFdiscoveryResult.png 
 
Now we can see in the above image that two endpoints are running and we are only using the first one.


Similar Articles