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>