So in WCF 4.0 Feature, we discussed Simplified Configuration , Standard End Points, File-less Activation and Protocol bridging and Fault tolerance. Now let's move to next.
Dynamic Service and End Point discovery
Dynamic Service and End Point discovery
The next major WCF 4 feature we’re going to discuss is service discovery. In some specialized service oriented environments, there are services whose run-time location is dynamic and constantly changing. Even 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.
WS-Discovery defines two primary modes of operation: ad hoc mode and managed mode. In ad hoc mode, clients probe for services by sending multicast messages. The framework provides UDP multicast mechanism for this ad-hoc mode. Services that match the probe respond directly to the client. In order to minimize the need for client polling, services can also "announce" themselves when joining or leaving the network by sending a multicast message to clients who may be "listening". Ad hoc discovery is limited by the protocol used for multicasting messages, in the case for UDP only the services listening in on the local subnet will be able to receive the messages.
With managed service discovery, you provide a discovery proxy on the network that “manages” the discoverable service endpoints. Clients talk directly to the discovery proxy to locate services based on probing criteria. The discovery proxy needs a repository of services that it can match against the query. How the proxy is populated with this information is an implementation detail. Discovery proxies can easily be connected to an exisiting service repository, they can be pre-configured with a list of endpoints, or a discovery proxy can even listen for announcements to update its cache. In managed mode, announcements can be unicast directly to a recipient, potentially by a discovery proxy.
Simple Service Discovery
The easiest way to enable service discovery is through the ad hoc mode.To configure your service for discovery, simply add the standard “udpDiscoveryEndpoint” endpoint and then enable the <serviceDiscovery> behavior on the service.
Here’s a complete example illustrating how to do this:
<configuration>
<system.serviceModel>
<services>
<service name="Service1">
<endpoint binding="wsHttpBinding" contract="IService1" />
<!-- add a standard UDP discovery endpoint-->
<endpoint name="udpDiscovery" kind="udpDiscoveryEndpoint"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDiscovery/> <!-- enable service discovery behavior -->
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
By doing this, your service becomes discoverable over UDP on the local subnet. Clients can then take advantage of WS-Discovery at runtime to “discover” the actual address of the running service. WCF 4 makes it easy for clients to accomplish this through the dynamicEndpoint standard endpoint.
Simply take your existing client endpoint that you were using to connect to the service, remove the address and add a kind=”dynamicEndpoint” tag.
<configuration>
<system.serviceModel>
<client>
<endpoint
name="ServiceEndpoint"
kind="dynamicEndpoint"
binding="wsHttpBinding"
contract="IService1">
</endpoint>
</client>
</system.serviceModel>
</configuration>
When the first service call is made, the client will send out a multicast query looking for services that match the ICalculatorService contract and attempt to connect to one. Various settings allow you to fine tune your serach, adjust the discovery bindings and control the discovery process.At Client side :-
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);
}
}
}
In this article, We discussed Dynamic Service and End Point Discovery. In later articles, I will cover in detail other features as well. Till then, thanks for reading...
Courtesy: Random Web Images, MSDN, Microsoft .NET 4.0 WCF Book, Several online resources
No comments:
Post a Comment