NET 4.0 : Enabling WCF service for discovery (Discovery Part III)

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

In this post I described how .NET 4.0 clients can make a usage of WCF-discovery functionality. This post describes how to enable WCF-service to be discoverable. In order for a service to be discoverable in an ad-hoc manner it needs to respond to so called probe messages. Ad-hoc discovery implies that these probe messages come in through a well known port over UDP multicast (DISCOVERY_PORT 3702 [IANA], IPv4 multicast address: 239.255.255.250 and IPv6 multicast address: FF02::C (link-local scope). Discovery APIs (System.ServiceModel.Discovery) allow you to add this functionality either imperatively or through configuration.
If you are interested on discovery details this post describes some important topics of WS-discovery protocol specification.

To enable discovery of the service by using of the configuration you need to do two things:

1. Add one endpoint with attribute kind = “udpDiscoveryEndpoint”:

<endpoint kind="udpDiscoveryEndpoint"/>

2. Add one Service Behavior which contains a serviceDiscvery element:

<serviceBehaviors>
   <behavior name="WcfServiceLibrary.Service1Behavior">
       <serviceDiscovery>
        </serviceDiscovery>
     <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="false" />
   </behavior>
</serviceBehaviors>

Next picture shows the final configuration. Note that this picture also contains announcementBinding,
which is no further described in this post.

image


Personally I do not like very much the way how the configuration in this case  is designed. For example, assuming that MEX requires httpMexBinding and interface IMetadataExchange I would expect that all other “special endpoints” work the same way. Unfortunately or even fortunately discovery works different way. You do not need any kind of discoveryBinding. To me this is not consistent and something should be changed. How about kind=”MEX”?

Behavior Extensions

As long your client connects to well know fixed endpoint (.NET < 4.0) the client might have some knowledge of the endpoint. However, when the endpoint is discovered, the client is obtaining the knowledge about endpoint dynamically. For this reason it is useful to inject more metadata in the endpoint. This data could represent some kind of description (extension) of the endpoint.

Following example shows how to add such extensions:

             foreach (System.ServiceModel.Description.ServiceEndpoint ep in host.Description.Endpoints){
              if (ep.Contract.ContractType == typeof(IService1))
              {
                  if (ep.Binding is BasicHttpBinding)
                  {
                      System.ServiceModel.Discovery.EndpointDiscoveryBehavior endpointDiscoveryBehavior = new
                        System.ServiceModel.Discovery.EndpointDiscoveryBehavior();

                      endpointDiscoveryBehavior.Extensions.Add(new XElement("MyExtensionRoot", new XElement
                       ("CevapNode", "Cevapi preko basic bindinga")));
                     
                      endpointDiscoveryBehavior.Extensions.Add(new XElement("MyExtensionRoot", new XElement
                       ("MyNode", "Using of basic binding.")));

                      ep.Behaviors.Add(endpointDiscoveryBehavior);
                  }
                  else if (ep.Binding is WSHttpBinding)
                  {
                      System.ServiceModel.Discovery.EndpointDiscoveryBehavior endpointDiscoveryBehavior = new
                          System.ServiceModel.Discovery.EndpointDiscoveryBehavior();

                      endpointDiscoveryBehavior.Extensions.Add(new XElement("MyExtensionRoot", new XElement
                      ("CevapNode", "Cevapi preko WS* bindinga")));
                     
                       endpointDiscoveryBehavior.Extensions.Add(new XElement("MyExtensionRoot", new XElement
                       ("MyNode", "Using of WSHttpBinding")));

                      ep.Behaviors.Add(endpointDiscoveryBehavior);
                  }
              }
          }

 
Next picture shows where the client can find this information:

image

 

Related Posts:

1. Part I: WCF and Service Discovery in .NET 4.0
2. Part II: NET 4.0 : Enabling WCF service for discovery
 

Visit www.daenet.de


Posted Nov 27 2008, 06:24 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.