WCF follow-up trip (15) logical address and Physical address

Source: Internet
Author: User

In WCF, each endpoint contains two different addresses-the logical address and the physical address. The logical address is the address represented by the endpoint addressing property. As for the physical address, for the message to send to say, is the message is really sent the destination address, and for the reception of the message, is the listener really listen to the address.

1, the service side of the physical address

By default, the logical and physical addresses of endpoints are the same URI. In other words, the logical address of the end is necessary, and how the physical address is not specified, using the logical address as the physical address by default. For the endpoint of the message receiver, the physical address is the listener address, expressed through the ServiceEndpoint ListenUri:

//---------------------------------------------------------------
// EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
//---------------------------------------------------------------
public class ServiceEndpoint
{
  ... ...
  public Uri ListenUri { get; set; }
}

When boarding a service, we can call the Serivicehostbase or ServiceHost addserviceendpoint corresponding overloads to specify ListenUri for the added endpoint:

//---------------------------------------------------------------
// EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
//---------------------------------------------------------------
public abstract class ServiceHostBase : CommunicationObject, IExtensibleObject<ServiceHostBase>, IDisposable
{
  ... ...
  public ServiceEndpoint AddServiceEndpoint(string implementedContract, Binding binding, string address, Uri listenUri);
  public ServiceEndpoint AddServiceEndpoint(string implementedContract, Binding binding, Uri address, Uri listenUri);
}

public class ServiceHost : ServiceHostBase
{
  ... ...
  public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address, Uri listenUri);
  public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, Uri address, Uri listenUri);
}

In the following code fragment, you specify a physical address (ListenUri) that is the same as the logical address for the endpoint:

//---------------------------------------------------------------
// ListenUri.cs (c) by 2008 Jiang Jin Nan
//---------------------------------------------------------------
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculateService)))
{
  serviceHost.AddServiceEndpoint(typeof(ICalculate),new WSHttpBinding(),
    "http://127.0.0.1:9999/calculateservice",
    new Uri ("http://127.0.0.1:8888/calculateservice"));
  Console.Read();
}

Of course, ListenUri can also be specified through configuration, and the following configuration is equivalent to the above code:

<configuration>
  <system.serviceModel>
    <services>
      <service name="Artech.WcfServices.Services.CalculateService">
        <endpoint binding="wsHttpBinding"
          contract="Artech.WcfServices.Contracts.ICalculate"  address="http://127.0.0.1:8888/calculateservice"
          listenUri="http://127.0.0.1:8888/calculateservice" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

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.