Partilhar via


Using a Custom Binding with the Discovery Client Channel

When using a custom binding with the DiscoveryClientBindingElement, you must define a DiscoveryEndpointProvider that creates DiscoveryEndpoint instances.

Creating a DiscoveryEndpointProvider

The DiscoveryEndpointProvider is responsible for creating DiscoveryEndpoints on demand. To define a discovery endpoint provider, derive a class from DiscoveryEndpointProvider and override the GetDiscoveryEndpoint method and return a new discovery endpoint. The following example shows how to create a discovery endpoint provider.

// Extend DiscoveryEndpointProvider class to change the default DiscoveryEndpoint
// to the DiscoveryClientBindingElement. The Discovery ClientChannel 
// uses this endpoint to send Probe message.
public class UdpDiscoveryEndpointProvider : DiscoveryEndpointProvider
{
   public override DiscoveryEndpoint GetDiscoveryEndpoint()
   {
      return new UdpDiscoveryEndpoint(DiscoveryVersion.WSDiscoveryApril2005);
   }
}

Once you have defined the discovery endpoint provider you can create a custom binding and add the DiscoveryClientBindingElement, which references the discovery endpoint provider as shown in the following example.

DiscoveryClientBindingElement discoveryBindingElement = new DiscoveryClientBindingElement();
            
// Provide the search criteria and the endpoint over which the probe is sent.
discoveryBindingElement.FindCriteria = new FindCriteria(typeof(ICalculatorService));
discoveryBindingElement.DiscoveryEndpointProvider = new UdpDiscoveryEndpointProvider();

CustomBinding customBinding = new CustomBinding(new NetTcpBinding());
// Insert DiscoveryClientBindingElement at the top of the BindingElement stack.
// An exception is thrown if this binding element is not at the top.
customBinding.Elements.Insert(0, discoveryBindingElement);

For more information about using the discovery client channel, see Using the Discovery Client Channel. For a complete code example, see Discovery Binding Element Sample

See Also

Tasks

Discovery Binding Element Sample

Concepts

WCF Discovery Overview
Using the Discovery Client Channel