Jump to content

.net 4.0 Loo Introduce Ayina Service Discovery Vidanalu


galigannarao

Recommended Posts

[b] Service Discovery[/b]

In [url="http://msdn.microsoft.com/en-us/library/ee354381.aspx"]WCF[/url] or the .NET Framework web services bonding between the client and the service is actually created only when the services are discovered and their ** reference is added to the client. If the URL of the hosted service is a fixed one, i.e., if it doesn't change over time the service can be discovered by "Add Web Reference" - for Web Services or "Add Service Reference" - for WCF. But for instance what if the service is designed to change its host URL quite frequently? Or what if the service would come on or off the network in a scheduled manner?
WCF 4.0 introduces a couple of service discovery techniques, which the client can use to discover the services dynamically through client probing or service announcement. WCF 4.0 comes up with the ability of dynamic discovery. This feature allows the client to probe for the services or reversely the services can also announce their presence on the network. Below are the dynamic service discovery techniques used in WCF 4.0.[list]
[*]Ad-Hoc or UDP Discovery
Managed Discovery
[/list]
[b] Ad-Hoc Discovery[/b]

Ad-Hoc discovery follows a broadcast model by sending multi cast request probe messages over the network and the matching services respond back with a probe match response to the client. The probe match response would have the endpoint details about the service.
In order to make a service discoverable, the .net framework 4.0 provides the endpoint class named UdpDiscoveryEndpoint lying under the System.ServiceModel.Discovery namespace. Also the service behavior named ServiceDiscoveryBehavior should be added to the service. From the client perspective, services can be discovered using the class DiscoveryClient. This class is responsible for issuing multi cast messages using [url="http://en.wikipedia.org/wiki/User_Datagram_Protocol"]UDP protocol[/url]. Services can be searched by providing the find criteria such as MaxResults to be returned, contract type, scope, etc. Only the matching services would send back a probe match response to the client.

Dynamic service discovery can be implemented on both the client and WCF service using the configuration file or through code behind. The config file option provides the capability of converting the existing WCF services to be dynamically discovered. Below is the sample code demonstrating the ad-hoc WCF service discovery.

[b]Service Host[/b]:
[color=blue]namespace[/color] AdhocServiceHost
{
[color=blue]class[/color] [color=#2B91AF]Program[/color]
{
[color=blue]static[/color] [color=blue]void[/color] Main([color=blue]string[/color][] args)
{
[color=#2B91AF]Uri[/color] hostAddress = [color=blue]new[/color] [color=#2B91AF]Uri[/color]([color=#A31515]"http://localhost:8888/GreetingService"[/color]);

[color=blue]using[/color] ([color=#2B91AF]ServiceHost[/color] serviceHost = [color=blue]new[/color] [color=#2B91AF]ServiceHost[/color]([color=blue]typeof[/color](GreetingService.Implementation.[color=#2B91AF]GreetingService[/color]), hostAddress))
{
[color=green]//Add a greeting endpoint[/color]
serviceHost.AddServiceEndpoint([color=blue]typeof[/color]([color=#2B91AF]IGreetingService[/color]), [color=blue]new[/color] [color=#2B91AF]WSHttpBinding[/color](), [color=#A31515]"http://localhost:8888/GreetingService/Greeting"[/color]);
[color=#2B91AF]ServiceMetadataBehavior[/color] metaDataBehavior = [color=blue]new[/color] [color=#2B91AF]ServiceMetadataBehavior[/color]();
metaDataBehavior.HttpGetEnabled = [color=blue]true[/color];
serviceHost.Description.Behaviors.Add(metaDataBehavior);

[color=green]//Add the ServiceDiscoveryBehavior[/color]
serviceHost.Description.Behaviors.Add([color=blue]new[/color] [color=#2B91AF]ServiceDiscoveryBehavior[/color]());

[color=green]//Add the discovery endpoint[/color]
serviceHost.AddServiceEndpoint([color=blue]new[/color] [color=#2B91AF]UdpDiscoveryEndpoint[/color]());

[color=green]//Open the host[/color]
serviceHost.Open();

[color=#2B91AF]Console[/color].WriteLine([color=#A31515]"Greeting Service Hosting URL: {0}"[/color], hostAddress);

[color=#2B91AF]Console[/color].WriteLine([color=#A31515]"Press any key to close the service"[/color]);
[color=#2B91AF]Console[/color].ReadLine();
}
}
}
}

[b]Client code[/b]:
[color=blue]namespace[/color] AdhocServiceClient
{
[color=blue]class[/color] [color=#2B91AF]Program[/color]
{
[color=blue]static[/color] [color=blue]void[/color] Main([color=blue]string[/color][] args)
{
[color=#2B91AF]DiscoveryClient[/color] discoveryClient = [color=blue]new[/color] [color=#2B91AF]DiscoveryClient[/color]([color=blue]new[/color] [color=#2B91AF]UdpDiscoveryEndpoint[/color]());

[color=green]//Set the probe criteria[/color]
[color=#2B91AF]FindCriteria[/color] findCriteria = [color=blue]new[/color] [color=#2B91AF]FindCriteria[/color]([color=blue]typeof[/color]([color=#2B91AF]IGreetingService[/color]));
[color=green]//Only the first result will be returned.[/color]
findCriteria.MaxResults = 1;

[color=#2B91AF]Collection[/color]<[color=#2B91AF]EndpointDiscoveryMetadata[/color]> endpointCollection = discoveryClient.Find(findCriteria).Endpoints;
discoveryClient.Close();

[color=blue]if[/color] (endpointCollection.Count > 0)
{
[color=#2B91AF]GreetingServiceClient[/color] client = [color=blue]new[/color] [color=#2B91AF]GreetingServiceClient[/color]([color=blue]new[/color] [color=#2B91AF]WSHttpBinding[/color](), endpointCollection[0].Address);
client.Greet();
}
[color=blue]else[/color]
[color=#2B91AF]Console[/color].WriteLine([color=#A31515]"No matching services discovered!"[/color]);

[color=#2B91AF]Console[/color].ReadLine();
}
}
}

This discovery technique is very useful when the service and client are lying in the same subnet. UDP (User Datagram Protocol) is the one that supports sending a multicast message to all on a local network.
Additionally, the WCF 4.0 service discovery feature allows the WCF service to announce its presence over the network.
[b] Managed Discovery[/b]

Unlike the ad-hoc discovery, managed discovery can be used to probe for services over a broader network. When I say broader network it includes the internet as well. In the managed discovery model, apart from the client and the service, there is an intermediate element called the DiscoveryProxyService.
It works like this, the service will make the announcement of its current state (online or offline) to the DiscoveryProxyService, which is hosted on a separate server and the DiscoveryProxyService takes care of caching the endpoint details in it. Developers can create their own DicoveryProxyService by deriving their class from the in-build DiscoveryProxy class.
Clients will send the unicast probe message directly to the DiscoveryProxyService and the later takes care of providing the probe match response back to the client based on the service endpoint information that it has cached.
Hope this article was informative. Happy reading!

Link to comment
Share on other sites

[color=#000000][font=arial][size=1]nenu inka 3.5 sakkaga use cheyyaledhu. appudey 4.0 vachindi. adhi start cheyyadam use chesariki 4.5 vasthademo.[/size][/font][/color][color=#000000][font=arial][size=1]
anduke msft tech's lo pani seyyakoodadhu. update sesukonikey sagam jeevitham sankanakipothadi[/size][/font][/color]

Link to comment
Share on other sites

×
×
  • Create New...