Configuring Discovery in a Configuration File

There are four major groups of configuration settings used in discovery. This topic will briefly describe each and show examples of how to configure them. Following each section will be a link to more in-depth documentation about each area.

Behavior Configuration

Discovery uses service behaviors and endpoint behaviors. The ServiceDiscoveryBehavior behavior enables discovery for all of a service's endpoints and allows you to specify announcement endpoints. The following example shows how to add the ServiceDiscoveryBehavior and specify an announcement endpoint.

<behaviors>
      <serviceBehaviors>
        <behavior name="helloWorldServiceBehavior">
          <serviceDiscovery>
            <announcementEndpoints>
              <endpoint kind="udpAnnouncementEndpoint"/>
            </announcementEndpoints>
          </serviceDiscovery>
        </behavior>
      </serviceBehaviors>
</behaviors>

Once you specify the behavior, reference it from a <service> element as shown in the following sample.

<system.serviceModel>
   <services>
      <service name="HelloWorldService" behaviorConfiguration="helloWorldServiceBehavior">
         <!-- Application Endpoint -->
         <endpoint address="endpoint0"
                   binding="basicHttpBinding"
                   contract="IHelloWorldService" />
         <!-- Discovery Endpoints -->
         <endpoint kind="udpDiscoveryEndpoint" />
        </service>
    </services>
</system.serviceModel>

In order for a service to be discoverable, you must also add a discovery endpoint, the example above adds a UdpDiscoveryEndpoint standard endpoint.

When you add announcement endpoints you must also add an announcement listener service to the <services> element as shown in the following example.

<services>
   <service name="HelloWorldService" behaviorConfiguration="helloWorldServiceBehavior">
      <!-- Application Endpoint -->
      <endpoint address="endpoint0"
                binding="basicHttpBinding"
                contract="IHelloWorldService" />
      <!-- Discovery Endpoints -->
      <endpoint kind="udpDiscoveryEndpoint" />
   </service>
   <!-- Announcement Listener Configuration -->
   <service name="AnnouncementListener">
      <endpoint kind="udpAnnouncementEndpoint" />
   </service>
</services>

The EndpointDiscoveryBehavior behavior is used to enable or disable discovery of a specific endpoint. The following example configures a service with two application endpoints, one with discovery enabled and one with discovery disabled. For each endpoint an EndpointDiscoveryBehavior behavior is added.

<system.serviceModel>
   <services>
      <service name="HelloWorldService"
               behaviorConfiguration="helloWorldServiceBehavior">

        <!-- Application Endpoints -->
        <endpoint address="endpoint0"
                 binding="basicHttpBinding"
                 contract="IHelloWorldService"
                 behaviorConfiguration="ep0Behavior" />

        <endpoint address="endpoint1"
                  binding="basicHttpBinding"
                  contract="IHelloWorldService"
                  behaviorConfiguration="ep1Behavior" />

        <!-- Discovery Endpoints -->
        <endpoint kind="udpDiscoveryEndpoint" />
      </service>
   </services>
   <behaviors>
      <serviceBehaviors>
        <behavior name="helloWorldServiceBehavior">
          <serviceDiscovery />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="ep0Behavior">
          <endpointDiscovery enabled="true"/>
        </behavior>
        <behavior name="ep1Behavior">
          <endpointDiscovery enabled="false"/>
        </behavior>
     </endpointBehaviors>
   </behaviors>
</system.serviceModel>

The EndpointDiscoveryBehavior behavior can also be used to add custom metadata to the endpoint metadata returned by the service. The following example shows how to do this.

<behavior name="ep4Behavior">
   <endpointDiscovery enabled="true">
      <extensions>
         <CustomMetadata>This is custom metadata.</CustomMetadata>
         <d:Types xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery" xmlns:i="http://printer.example.org/2003/imaging">i:PrintBasic</d:Types>
         <CustomMetadata nested="true">
            <NestedMetadata>This is a nested custom metadata.</NestedMetadata>
         </CustomMetadata>
      </extensions>
   </endpointDiscovery>
</behavior>

The EndpointDiscoveryBehavior behavior can also be used to add scopes and types that clients use to search for services. The following example shows how to do this in a client side configuration file.

<behavior name="ep2Behavior">
   <endpointDiscovery enabled="true">
      <scopes>
         <add scope="http://www.microsoft.com/building42/floor1"/>
         <add scope="ldap:///ou=engineeringo=examplecomc=us"/>
      </scopes>
      <types>
         <add name="test" namespace="http://example.microsoft.com/" />
         <add name="additionalContract" namespace="http://example.microsoft.com/" />
      </types>
   </endpointDiscovery>
</behavior>

For more information about ServiceDiscoveryBehavior and EndpointDiscoveryBehavior see WCF Discovery Overview.

Binding Element Configuration

Binding element configuration is most interesting on the client side. You can use configuration to specify the find criteria used to discover services from a WCF client application. The following example creates a custom binding with the DiscoveryClient channel and specifies find criteria that includes a type and scope. In addition it specifies values for the Duration and MaxResults properties.

<bindings>
   <customBinding>
      <!-- Binding Configuration for the Application Endpoint -->
      <binding name="discoBindingConfiguration">
         <discoveryClient>
            <endpoint kind="discoveryEndpoint"
                      address="http://localhost:8000/ConfigTest/Discovery"
                      binding="customBinding"
                      bindingConfiguration="httpSoap12WSAddressing10"/>
            <findCriteria duration="00:00:10" maxResults="2">
              <types>
                <add name="IHelloWorldService"/>
              </types>
              <scopes>
                <add scope="http://www.microsoft.com/building42/floor1"/>
              </scopes>
            </findCriteria>
          </discoveryClient>
          <textMessageEncoding messageVersion="Soap11"/>
          <httpTransport />
      </binding>
   </customBinding>
</bindings>

This custom binding configuration must be referenced by a client endpoint:

<client>
      <endpoint address="http://schemas.microsoft.com/discovery/dynamic"
                binding="customBinding"
                bindingConfiguration="discoBindingConfiguration"
                contract="IHelloWorldService" />
</client>

For more information about find criteria see Discovery Find and FindCriteria. For more information about discovery and binding elements see, WCF Discovery Overview

Standard Endpoint Configuration

Standard endpoints are predefined endpoints that have default values for one or more properties (address, binding, or contract) or one or more property values that cannot change. .NET 4 ships with 3 discovery related standard endpoints: UdpDiscoveryEndpoint, UdpAnnouncementEndpoint, and DynamicEndpoint. The UdpDiscoveryEndpoint is a standard endpoint that is pre-configured for discovery operations over a UDP multicast binding. The UdpAnnouncementEndpoint is a standard endpoint that is pre-configured to send announcement messages over a UDP binding. The DynamicEndpoint is a standard endpoint that uses discovery to find the endpoint address of a discovered service dynamically at run time. Standard bindings are specified with an <endpoint> element that contains kind attribute that specified the type of standard endpoint to add. The following example shows how to add a UdpDiscoveryEndpoint and a UdpAnnouncementEndpoint.

<services>
   <service name="HelloWorldService">
      <!-- ...  -->
      <endpoint kind="udpDiscoveryEndpoint" />
   </service>
   <service name="AnnouncementListener">
      <endpoint kind="udpAnnouncementEndpoint" />
   </service>
</services>

Standard endpoints are configured in a <standardEndpoints> element. The following example shows how to configure the UdpDiscoveryEndpoint and the UdpAnnouncementEndpoint.

<standardEndpoints>
      <udpAnnouncementEndpoint>
        <standardEndpoint
            name="udpAnnouncementEndpointSettings"
            multicastAddress="soap.udp://239.255.255.250:3703"
            maxAnnouncementDelay="00:00:00.800">
          <transportSettings
            duplicateMessageHistoryLength="1028"
            maxPendingMessageCount="10"
            maxMulticastRetransmitCount="3"
            maxUnicastRetransmitCount="2"
            socketReceiveBufferSize="131072"
            timeToLive="2" />
        </standardEndpoint>
      </udpAnnouncementEndpoint>
      <udpDiscoveryEndpoint>
        <standardEndpoint
            name="udpDiscoveryEndpointSettings"
            multicastAddress="soap.udp://239.255.255.252:3704"
            maxResponseDelay="00:00:00.800">
          <transportSettings
            duplicateMessageHistoryLength="2048"
            maxPendingMessageCount="5"
            maxReceivedMessageSize="8192"
            maxBufferPoolSize="262144"/>
        </standardEndpoint>
      </udpDiscoveryEndpoint>
</standardEndpoints>

Once you've added the standard endpoint configuration, reference the configuration in the <endpoint> element for each endpoint as shown in the following sample.

<services>
   <service name="HelloWorldService">
      <!-- ...  -->
      <endpoint kind="udpDiscoveryEndpoint" endpointConfiguration="udpDiscoveryEndpointSettings"/>
   </service>
   <service name="AnnouncementListener">
      <endpoint kind="udpAnnouncementEndpoint" endpointConfiguration="udpAnnouncementEndpointSettings" />
   </service>
</services>

Unlike the other standard endpoints used in discovery, you specify a binding and contract for DynamicEndpoint. The following example shows how to add and configure a DynamicEndpoint.

<system.serviceModel>
    <client>
      <endpoint kind="dynamicEndpoint" binding="basicHttpBinding" contract="IHelloWorldService" endpointConfiguration="dynamicEndpointConfiguration" />
    </client>
   <standardEndpoints>
      <dynamicEndpoint>
         <standardEndpoint name="dynamicEndpointConfiguration">
             <discoveryClientSettings>
              <findCriteria scopeMatchBy="http://schemas.microsoft.com/ws/2008/06/discovery/rfc" duration="00:00:10" maxResults="2">
                 <types>
                   <add name="IHelloWorldService"/>
                 </types>
                 <scopes>
                   <add scope="http://www.microsoft.com/building42/floor1"/>
                 </scopes>
                 <extensions>
                   <CustomMetadata>This is custom metadata.</CustomMetadata>
                 </extensions>
               </findCriteria>
             </discoveryClientSettings>
           </standardEndpoint>
         </dynamicEndpoint>
   </standardEndpoints>
</system.serviceModel>

For more information about standard endpoints see Standard Endpoints.