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.
public interface IService1
string GetData(int value);
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());
Console.WriteLine("Press to terminate service.");
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;
if (FindService()) InvokeService();
static bool FindService()
Console.WriteLine("\nFinding Myservice Service ..");
DiscoveryClient discoveryClient =
new DiscoveryClient(new UdpDiscoveryEndpoint());
discoveryClient.Find(new FindCriteria(typeof(ServiceReference1.IService1)));
Console.WriteLine("\nNo services are found.");
serviceAddress = Services.Endpoints[0].Address;
static void InvokeService()
Console.WriteLine("\nInvoking My Service at {0}\n", serviceAddress);
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.Endpoint.Address = serviceAddress;
Step 4: Now with the help of Set Startup Project Wizard we will start multiple projects:
Step 5: Now we will see the service host output:
In the following image we can see how our service is running:
Step 6: Now finally will we run our client application and will see how many endpoints are running:
Now we can see in the above image that two endpoints are running and we are only using the first one.