The Microsoft. NET Framework 3.5 has two limitations for all possible calls to Windows communication Foundation (WCF). The first limitation is that the port or pipe assigned to the service must be available. Therefore, an application developer or administrator must imagine or provide some way to retain these ports or pipelines. The second limitation is that the client must know the service endpoint address in advance, including the port number and the server or pipe name.
Ideally, the service will be able to use any of the available addresses. In turn, the client needs to discover this address at run time. In fact, there is an industry-standard solution to specify how this discovery is positioned. The topic of this column is the solution ("discovery") and its support mechanism. Also, I'll introduce a few useful tools and helper classes. The source code for this content can be found in code.msdn.
Address discovery
Discovery relies on User Datagram Protocol (UDP). Unlike Transmission Control Protocol (TCP), UDP is a connectionless protocol and there is no need to establish a direct connection between the sender and receiver of the packet. The client uses UDP to propagate a discovery request to any endpoint that supports the specified contract type. The specialized discovery endpoints supported by the service will receive these requests. The implementation of the discovery endpoint responds to the client to provide the address of the service endpoint that supports the specified convention. The client discovers the service and then invokes it, the same as a regular WCF call. For this procedure, the demo is shown in Figure 1.
Figure 1 Discovery address via UDP
Very similar to the metadata interchange (MEX) endpoint, WCF provides a standard discovery endpoint of type udpdiscoveryendpoint:
public class DiscoveryEndpoint : ServiceEndpoint
{...}
public class UdpDiscoveryEndpoint : DiscoveryEndpoint
{...}
You can enable the host to implement the endpoint by adding Servicediscoverybehavior to the set of behaviors that the service supports. You can do this programmatically in the following ways:
ServiceHost host = new ServiceHost(...);
host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
ServiceDiscoveryBehavior discovery = new ServiceDiscoveryBehavior();
host.Description.Behaviors.Add(discovery);
host.Open();
Figure 2 shows how to use the service profile to add discovery endpoints and Discovery behavior.
Figure 2 Adding the discovery endpoint in the configuration file
<services>
<service name = "MyService">
<endpoint
kind = "udpDiscoveryEndpoint"
/>
...
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDiscovery/>
</behavior>
</serviceBehaviors>
</behaviors>