Monday, April 7, 2014

WCF Discovery : Ad Hoc Discovery Client

WCF Discovery is a new feature which was shipped with WCF 4.0.WCF discovery is an implementation of the OASIS WS-Discovery specification.Think a scenario where  client application has the address of a specific service hard-coded into its configuration, then if that service moves, becomes temporarily unavailable, or is just too busy to handle requests, the client will not be able to communicate with it. WCF provides discovery and routing to address these issues. By implementing discovery client Application can locate the specific service address dynamically. As well as service can broadcasts its status to the all clients when service goes down or up.

There are two ways to implement WCF discovery.

  • Add Hoc Discovery
  • Manage Discovery

In this example I m going to explain Add Hoc Discovery client.In this client, service address not hard coded.Instead of that setting up the client endpoint address dynamically.

How this works ?

In Adhoc model clients send Probe message to the network .This message contains the information about the service which it wishes to connect. After service receives a Probe request, it can examine its contents, and if the probe matches the contract implemented by the service, it can respond to the client with a ProbeMatch message. TheProbeMatch message contains the service addressing information that the client needs to connect to the service.Client message uses the UDP protocol to send the probe message.

When implementing in order to  support above model service need to add specific endpoint call udpDiscoveryEndpoint and Service Discovery behavior to the service behaviors

what is udpDiscoveryEndpoint – This is a standard endpoint which used to implement the ad hoc mode of discovery.

<services>
<service name="Discovery.PTSService" behaviorConfiguration="discoveryBehaviour">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpBinding"
contract="Discovery.IPTSService" />
<!—new discovery endpoint-->
<endpoint kind="udpDiscoveryEndpoint"/>
</service>
</services>


What is Kind ?


udpDiscoveryEndpoint is standard endpoint .if we need to use standard endpoint we have to use kind instead of name. for more information http://msdn.microsoft.com/en-us/library/ee358762(v=vs.110).aspx


<serviceBehaviors>
<behavior name="discoveryBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />

<!-- service dicovery behaviors -->
<serviceDiscovery />
</behavior>
</serviceBehaviors>


Additionally we can set the scope for the service endpoint behaviors.which Adds the scope information for the endpoint that can be used in matching criteria for finding services.


Now our service is completed.(please make sure service is up and running)Now move the client implementation.


image


Client need to have reference to the System.ServiceModel.Discovery and your service.


image


static void Main(string[] args)
{
//initialize new discovery Client with UdpDiscoveryEndpoint
DiscoveryClient client = new DiscoveryClient(new UdpDiscoveryEndpoint());

// define the find criteria with serivce type
FindCriteria criteria = new FindCriteria(typeof(IPTSService));

// then clinet .Find
FindResponse services = client.Find(criteria);

client.Close();
}


Additionally FindCriteria can set the time out for service discovery. whether criteria can find service or not its return the output after the specified time.


criteria.Duration = TimeSpan.FromSeconds(50);


Lets run and see what happens.I have put watcher to the my services object..


image


Now you can see Find response contains all the required for the service invocation.

1 comment: