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.
A Note:
On 22nd of July 2009, I got a very sweet and new gift from my GOD. So I thought why I should not start something which is new for me on this auspicious day? And I started WCF 4.0. I welcome that gift in my life and welcome WCF4.0 too in my learning. Thanks God.
What are the new in WCF 4.0?
I am listing here, new features in WCF 4.0
- Dynamic Service and End Point discovery
- Intermediate Routing Pattern ( Generic Routing Services)
- Discovery announcement
- Simplified Configuration
- Protocol bridging and Fault tolerance
- Standard End Points.
- .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
- This is a multicast protocol that issues SOAP message over UDP,
- 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.
- 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.
- 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:
- 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.
- When a new service starts up it sends an announcement message to the discovery proxy.
- The discovery proxy saves information about each available service to storage.
- 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.
- If there are matches the discovery proxy sends a ProbeMatch response back to the client.
- The client can then contact the service directly using the service information returned from the proxy.
Ad-Hoc Mode:
- There is no centralized server.
- Service announcements and client requests are sent in a multicast fashion.
- 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.
- Therefore, clients have to actively listen for these announcements and process them accordingly.
- When a client issues a Probe request for a service it is also sent over the network using a multicast protocol.
- 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.
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">
<!-- 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
- One new End Point is being added.
- Kind of newly added End Point is udpDiscoveryEndpoint.
- This End Point will be used to discover the service.
- ServiceDiscovery behavior is also get added.
Step 4:
Press F5 to run the service. Service will get hosted in ASP.Net web server.
Step 5:
Add a new console project in solution.
Step 6:
Add Reference of System.ServiceModel.Discovery.dll in console project.
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
-
We are making object of DiscoveryClient class. This class is user to discover available service.
-
In constructor of this class, we are passing UdpDiscoveryEndPoint of the service.
-
Then we are calling Find method with contract name as argument to get FindResponse.
-
Now FindResponse variable having all the available address for contract passed in Find method.
-
We are creating the client proxy and passing discovered address as argument.
-
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.
Happy Coding!