Base Content-Find new WCF by discovery

Source: Internet
Author: User
Tags port number

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.