Reader Level:
ARTICLE

WCF Discovery in .NET 4.0

Posted by Amit Patel Articles | WCF with C# June 30, 2012
Windows Communication Foundation has introduced a new feature of WCF; Discovery and with the help of this feature we can access the other service.
  • 0
  • 0
  • 10246
Download Files:
 

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.

Purpose

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 a 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: http://msdn.microsoft.com/en-us/library/dd456782

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.

[ServiceContract]

public interface IService1

{

    [OperationContract]

    string GetData(int value);


    [OperationContract]

    CompositeType GetDataUsingDataContract(CompositeType composite);

}

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.

static void Main(string[] args)

{

     Uri baseAddress = new Uri(string.Format("http://{0}:8000/discovery/scenarios/Myservice/{1}/",

     System.Net.Dns.GetHostName(), Guid.NewGuid().ToString()));

     Console.WriteLine(baseAddress);

     using (ServiceHost serviceHost = new ServiceHost(typeof(SampleDicoveryService.Service1), baseAddress))

     {

          serviceHost.AddServiceEndpoint(typeof(SampleDicoveryService.IService1), new WSHttpBinding(), string.Empty);

          serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());

          serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

          serviceHost.Open();

          Console.WriteLine("Press to terminate service.");

          Console.ReadLine();

     }

}

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.

static EndpointAddress serviceAddress;

static void Main()

{

    if (FindService()) InvokeService();

}

static bool FindService()

{

    Console.WriteLine("\nFinding Myservice Service ..");

    DiscoveryClient discoveryClient =
          
new DiscoveryClient(new UdpDiscoveryEndpoint());

    var Services =

         discoveryClient.Find(new FindCriteria(typeof(ServiceReference1.IService1)));

    discoveryClient.Close();

    if (Services == null)

    {

         Console.WriteLine("\nNo services are found.");

         return false;

    }

    else

    {

         serviceAddress = Services.Endpoints[0].Address;

         return true;

    }

}

static void InvokeService()

{

     Console.WriteLine("\nInvoking My Service at {0}\n", serviceAddress);

     ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

     client.Endpoint.Address = serviceAddress;

     client.GetData(1);
}

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.

COMMENT USING

Trending up