WCF后续之旅(15): 逻辑地址和物理地址
在WCF中,每个终结点都包含两个不同的地址——逻辑地址和物理地址。逻辑地址就是终结点Address属性表示的地址。至于物理地址,对于消息发送放来讲,就是消息被真正发送的目的地址;而对于消息的接收放来讲,就是易做图真正监听的地址。
一、服务端的物理地址
在默认的情况下,终结点的逻辑地址和物理地址是同一个URI。换句话说,终结的逻辑地址是必须的,如何物理地址没有指定的,默认使用逻辑地址作为物理地址。对于消息接收方的终结点来讲,物理地址就是监听地址,通过ServiceEndpoint的ListenUri表示 :
1: //---------------------------------------------------------------
2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
3: //---------------------------------------------------------------
4: public class ServiceEndpoint
5: {
6: ... ...
7: public Uri ListenUri { get; set; }
8: }在对服务进行寄宿的时候,我们可以调用SeriviceHostBase或者ServiceHost的AddServiceEndpoint对应的重载来为添加的终结点指定ListenUri:
1: //---------------------------------------------------------------
2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
3: //---------------------------------------------------------------
4: public abstract class ServiceHostBase : CommunicationObject, IExtensibleObject<ServiceHostBase>, IDisposable
5: {
6: //... ...
7: public ServiceEndpoint AddServiceEndpoint(string implementedContract, Binding binding, string address, Uri listenUri);
8: public ServiceEndpoint AddServiceEndpoint(string implementedContract, Binding binding, Uri address, Uri listenUri);
9: }
10:
11: public class ServiceHost : ServiceHostBase
12: {
13: //... ...
14: public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address, Uri listenUri);
15: public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, Uri address, Uri listenUri);
16: }
17:在下面的代码片断中,就为终结点指定了一个同于逻辑地址的物理地址(ListenUri):
1: //---------------------------------------------------------------
2: // ListenUri.cs (c) by 2008 Jiang Jin Nan
3: //---------------------------------------------------------------
4: using (ServiceHost serviceHost = new ServiceHost(typeof(CalculateService)))
5: {
6: serviceHost.AddServiceEndpoint(typeof(ICalculate),new WSHttpBinding(),
7: "http://127.0.0.1:9999/calculateservice",
8: new Uri ("http://127.0.0.1:8888/calculateservice"));
9: Console.Read();
10: }
11:当然,ListenUri也可以通过配置进行指定,下面的配置和上面的代码是等效的:
1: <configuration>
2: <system.serviceModel>
3: <services>
4: <service name="Artech.WcfServices.Services.CalculateService">
5: <endpoint binding="wsHttpBinding"
6: contract="Artech.WcfServices.Contracts.ICalculate" address="http://127.0.0.1:8888/calculateservice"
7: listenUri="http://127.0.0.1:8888/calculateservice" />
8: </service>
9: </services>
10: </system.serviceModel>
11: </configuration>
12:二、客户端的物理地址
上面我们介绍了基于消息接收端终结点物理地址的指定,现在我们来介绍对于消息发送端的终结点,物理地址如何指定。在上面我们说过,对于消息的发送端来讲,物理地址其实就是消息发送的真正目的地址。该地址通过一个特殊的EndpointBehavior,ClientViaBehavor来指定。ClientViaBehavor定义的Uri代表该物理地址。
1: //---------------------------------------------------------------
2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
3: //---------------------------------------------------------------
4: public class ClientViaBehavior : IEndpointBehavior
5: {
6: //... ...
7: public Uri Uri { get; set; }
8: }
ClientViaBehavor是WCF自定的EndpointBehavior, 我们可以通过下面的配置应用该ClientViaBehavor。通过<endpointBehaviors>下的<clientVia〉配置节,通过viaUri设置了一个不同于终结点地址(http://127.0.0.1:9999/calculateservice)的物理地址:http://127.0.0.1:8888/calculateservice。1: <?xml version="1.0" encoding="utf-8" ?>
2: <configuration>
3: <system.serviceModel>
4: <behaviors>
5: <endpointBehaviors>
6: <behavior name="clientViaBehavior">
7: <clientVia viaUri="http://127.0.0.1:8888/calculateservice" />
8: </behavior>
9: </endpointBehaviors>
10: </behaviors>
11: <client>
12: <endpoint address="http://127.0.0.1:9999/calculateservice" behaviorConfiguration="clientViaBehavior"
13: binding="wsHttpBinding" bi
补充:综合编程 , 其他综合 ,