通过 DiscoveryClientBindingElement 使用自定义绑定时,必须定义创建 DiscoveryEndpoint 实例的 DiscoveryEndpointProvider。
创建 DiscoveryEndpointProvider
DiscoveryEndpointProvider 负责按要求创建 DiscoveryEndpoints。若要定义发现终结点提供程序,请从 DiscoveryEndpointProvider 派生类,然后覆盖 GetDiscoveryEndpoint 方法并返回新发现终结点。下面的示例演示如何创建发现终结点提供程序。
// 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);
}
}
一旦定义了发现终结点提供程序,即可创建自定义绑定并添加引用发现终结点提供程序的 DiscoveryClientBindingElement,如下面的示例所示。
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);
有关使用 Discovery 客户端通道的更多信息,请参见使用 Discovery 客户端通道。有关完整代码示例,请参见发现绑定元素示例