This article is 1st 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


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

Step1

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

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.

Step3

Open Web.Config file. And modify the System.ServiceModel. Add the code which in red rectangle below.

Web.Config

<system.serviceModel>

<services>

<service
name=WcfService1.Service1
behaviorConfiguration=WcfService1.Service1Behavior>

<!– Service Endpoints –>

<endpoint
address=“”
binding=wsHttpBinding
contract=WcfService1.IService1>

<!–

Upon deployment, the following identity element should be removed or replaced to reflect the

identity under which the deployed service runs. If removed, WCF will infer an appropriate identity

automatically.


–>

<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>

<!– To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment –>

<serviceMetadata
httpGetEnabled=true/>

<!– To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information –>

<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.

 

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


Step 4

Add a new console project in solution. Add Reference of System.ServiceModel.Discovery.dll in console project.


Step 5

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


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.

Advertisements

2 thoughts on “Dynamic Service and End Point Discovery feature of WCF 4.0

  1. How same thing can be done through config ?
    If i specifying everything through config in service i have specified in step 3 as above
    I have specified this in the client

    and in the client code i am calling the service through

    Service1Client proxy = new Service1Client(“Service1Endpoint”);
    proxy.somemethod();

    i am getting error The client could not discover any endpoints .

  2. How same thing can be done through config ?
    If i specifying everything through config in service i have specified in step 3 as above
    I have specified this in the client

    and in the client code i am calling the service through

    Service1Client proxy = new Service1Client(“Service1Endpoint”);
    proxy.somemethod();

    i am getting error The client could not discover any endpoints .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s