.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Dynamic Service and End Point Discovery feature of WCF 4.0

Posted By:Dhananjay Kumar       Posted Date: February 27, 2010    Points: 25    Category: WCF    URL: http://www.dotnetspark.com  

Dynamic Service and End Point Discovery feature of WCF 4.0. This article will list all the new feature of WCF and also will explain in detail Dynamic Service and End Point Discovery feature of WCF 4.0.
 

Objective:

This article is first part of multi series article on WCF 4.0. This article will list all the new feature of WCF and also will explain in detail Dynamic Service and End Point Discovery feature of WCF 4.0.


What are the new in WCF 4.0?

I am listing here, new features in WCF 4.0
  1. Dynamic Service and End Point discovery
  2. Intermediate Routing Pattern ( Generic Routing Services)
  3. Discovery announcement
  4. Simplified Configuration
  5. Protocol bridging and Fault tolerance
  6. Standard End Points.
  7. .svc-less activation of REST services or making REST URI nice.
Readers Note

I will discuss details of each feature in subsequent articles. This article is emphasizing on Dynamic Service and End Point discovery. So please tune in.

Dynamic Service and End Point discovery

Problem:

People say; you need to know only ABC of a service to expose End Point of that service. And client or service consumer needs to know only End Point to consume the service. But wait for a while here, and think; what if? Binding need to change at the service side from basic to WS over HTTP, This is very basic change but to accommodate this client has to update the service again. And this is very much error prone. It is very tedious task to update client about all the basic frequent change at the service side. To solve this issue, there should be some mechanism and that is called End Point Discovery or Dynamic Service.

In absolute technical words

WCF 4.0 supports WS - Discovery standard or protocol.

WS-Discovery Standard
  1. This is a multicast protocol that issues SOAP message over UDP,
  2. WS-Discovery is a Standard that defines a lightweight discovery mechanism for discovering services based on multicast messages. It enables a service to send a Hello announcement message when it is initialized and a Bye message when is removed from the network.
  3. Client or consumer can discover services by multicasting a Probe message to which a service can reply with a ProbeMatch message containing the information necessary to contact the service.
  4. Client or consumer can find services that have changed endpoint by issuing a Resolve message to which respond with a ResolveMatchmessage.
So, we can say WS - Discovery is a UDP based multicast message exchange. This message receives End Point information from Service and uses this as discovery information. Client uses discovery information to discover the available service on the network.

WCF Service Discovery API

WCF provides, WCF Discovery API for dynamic publish and discovery of web service using WS - Discovery protocol. This API helps service to publish them and helps client to find service on the network.

Modes:

1.gif


Managed Mode:
  1. In managed mode there is a centralized server called a discovery proxy that services use to publish themselves and clients use to retrieve information about available services.
  2. When a new service starts up it sends an announcement message to the discovery proxy.
  3. The discovery proxy saves information about each available service to storage.
  4. When a client must search for a service it sends a Probe request to the discovery proxy and it determines whether any of the services that have been published match the request.
  5. If there are matches the discovery proxy sends a ProbeMatch response back to the client.
  6. The client can then contact the service directly using the service information returned from the proxy.
Ad-Hoc Mode:
  1. There is no centralized server.
  2. Service announcements and client requests are sent in a multicast fashion.
  3. If a service is configured to send out a Hello announcement on start up, it sends it out over a well-known, multicast address using the UDP protocol.
  4. Therefore, clients have to actively listen for these announcements and process them accordingly.
  5. When a client issues a Probe request for a service it is also sent over the network using a multicast protocol.
  6. Each service that receives the request determines whether it matches the criteria in the Probe request and responds directly to the client with a ProbeMatch message if the service matches the criteria specified in the Probe request.
Sample

In this sample, we will create a very basic service and at the client side, first client will discover the service in Ad-Hoc mode and then consume that.
To run and go through this sample, you need Visual Studio 2010 Beta.

To make a service discoverable, a ServiceDiscoveryBehavior must be added to the service host and a discovery endpoint must be added to specify where to listen for and send discovery message

Step 1

Open VS2010. Create a new project. Select project type as Web. From Web Project type choose WCF Service Application Project template.

2.gif

Step 2

Delete all the code which is generated by Visual studio. After deleting Contract and Service Implantation is as follows.

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
  [ServiceContract]
  public interface IService1
  {
  [OperationContract]
  string GetMessage(string  msg);
  }
}

Service1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;  namespace WcfService1
{
  public class Service1 : IService1
  {
  public string GetMessage(string msg)
  {
  return msg; 
  }
  }
}

The above is simple Service Contract and implantation of that.

Step 3:

Open Web.Config file. And modify the System.ServiceModel. Add the code which is highlighted below.

Web.Config

<system.serviceModel>
  <
services>
  <
service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
 
  <
endpoint address="" binding="wsHttpBinding" contract="WcfService1.IService1"> 
 
  <
identity>
  <
dns value="localhost"/>
  identity>
  endpoint>  
 <
endpoint name ="udpDiscovery" kind ="udpDiscoveryEndpoint" />
   <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  service>
  services>
  <
behaviors>
  <
serviceBehaviors>
  <
behavior name="WcfService1.Service1Behavior">
 
  <
serviceMetadata httpGetEnabled="true"/>
 
  <
serviceDebug includeExceptionDetailInFaults="false"/>
   <serviceDiscovery />
   behavior>
  serviceBehaviors>
  behaviors>
  system.serviceModel>

Explanation of Config file
  1. One new End Point is being added.
  2. Kind of newly added End Point is udpDiscoveryEndpoint.
  3. This End Point will be used to discover the service.
  4. ServiceDiscovery behavior is also get added.
Step 4:

Press F5 to run the service. Service will get hosted in ASP.Net web server.

3.gif

Step 5:

Add a new console project in solution.

Step 6:

Add Reference of System.ServiceModel.Discovery.dll in console project.

4.gif

Step 7:

Add namespace using System.ServiceModel.Discovery;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.ServiceReference1;
using System.ServiceModel;
using System.ServiceModel.Discovery;  namespace ConsoleApplication1
{
  class Program
  {
  static void Main(string[] args)
  {
  DiscoveryClient discoverclient = new DiscoveryClient(new UdpDiscoveryEndpoint());
  FindResponse response = discoverclient.Find(new FindCriteria(typeof(IService1)));
  EndpointAddress address = response.Endpoints[0].Address;
  Service1Client client = new Service1Client(new  WSHttpBinding(), address);
  string str= client.GetMessage("Hello WCF 4 ");
  Console.WriteLine(str);
  Console.ReadKey(true);
  }
  }
}

Explanation of the code
  1. We are making object of DiscoveryClient class. This class is user to discover available service.
  2. In constructor of this class, we are passing UdpDiscoveryEndPoint of the service.
  3. Then we are calling Find method with contract name as argument to get FindResponse.
  4. Now FindResponse variable having all the available address for contract passed in Find method.
  5. We are creating the client proxy and passing discovered address as argument.
  6. Calling the service operation on the client proxy.
Output:

5.gif

Conclusion:

In this article, I talked about what are the new features in WCF 4.0. I went into details of Dynamic Service and End Point Discovery. In later articles, I will cover in detail other features as well. Till then, thanks for reading.

Happy Coding!

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend