閱讀987 返回首頁    go 阿裏雲 go 技術社區[雲棲]


WCF後續之旅(15): 邏輯地址和物理地址

在默認的情況下,終結點的邏輯地址和物理地址是同一個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:        "https://127.0.0.1:9999/calculateservice",
   8:        new Uri ("https://127.0.0.1:8888/calculateservice"));
   9:    Console.Read();
  10: } 
  11:  

   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="https://127.0.0.1:8888/calculateservice"
   7:                     listenUri="https://127.0.0.1:8888/calculateservice" />
   8:             </service>
   9:         </services>
  10:     </system.serviceModel>
  11: </configuration> 
  12:  

   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: } 

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:     <system.serviceModel>
   4:         <behaviors>
   5:             <endpointBehaviors>
   6:                 <behavior name="clientViaBehavior">
   7:                     <clientVia viaUri="https://127.0.0.1:8888/calculateservice" />
   8:                 </behavior>
   9:             </endpointBehaviors>
  10:         </behaviors>
  11:         <client>
  12:             <endpoint address="https://127.0.0.1:9999/calculateservice" behaviorConfiguration="clientViaBehavior"
  13:                 binding="wsHttpBinding" bindingConfiguration="" contract="Artech.WcfServices.Contracts.ICalculate"
  14:                 name="calculateservice">               
  15:             </endpoint>
  16:         </client>
  17:     </system.serviceModel>
  18: </configuration>

和ListenUriMode

   1: //---------------------------------------------------------------
   2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
   3: //---------------------------------------------------------------
   4: public enum ListenUriMode
   5: {
   6:     Explicit,
   7:     Unique
   8: } 

  • 如果采用TCP作為傳輸協議,同時采用端口共享情況下,會添加一個GUID作為後綴以確保地址的唯一性
  • 對於非TCP作為傳輸協議,會添加一個GUID作為後綴以確保地址的唯一性

   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:     public ListenUriMode ListenUriMode { get; set; }
   9: } 
  10:  

   1: //---------------------------------------------------------------
   2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
   3: //---------------------------------------------------------------
   4: using (ServiceHost serviceHost = new ServiceHost(typeof(CalculateService)))
   5: {    
   6:  
   7:     ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(ICalculate), new WSHttpBinding(),
   8:     http://127.0.0.1:9999/calculateservice,    new Uri("https://127.0.0.1:8888/calculateservice"));
   9:     endpoint.ListenUriMode = ListenUriMode.Unique;
  10:     Console.Read();
  11: } 
  12:  

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3: <system.serviceModel>
   4:         <services>
   5:             <service name="Artech.ListenUriDemos.Services.CalculateService">
   6:                 <endpoint address="https://127.0.0.1:9999/calculateservice"  binding="wsHttpBinding" contract="Artech.ListenUriDemos.Contracts.ICalculate"
   7: listenUriMode="Unique" listenUri="https://127.0.0.1:8888/calculateservice" />
   8:             </service>
   9:         </services>
  10:     </system.serviceModel>
  11: </configuration> 
  12:  

image

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:     <system.serviceModel>
   4:         <bindings>
   5:             <netTcpBinding>
   6:                 <binding name="PortSharingBinding" portSharingEnabled="true" />
   7:             </netTcpBinding>
   8:         </bindings>
   9:         <services>
  10:             <service name="Artech.ListenUriDemos.Services.CalculateService">
  11:                <!--1. BasicHttpBinding & ListenUriMode.Explicit-->
  12:                 <endpoint address="https://127.0.0.1:5555/service1" binding="basicHttpBinding"
  13:                     name="httpExplicitListenUriMode" contract="Artech.ListenUriDemos.Contracts.ICalculate" />
  14:                 <!--2. BasicHttpBinding & ListenUriMode.Unique-->
  15:                 <endpoint address="https://127.0.0.1:6666/service2" binding="basicHttpBinding"
  16:                     name="httpUniquListenUriMode" contract="Artech.ListenUriDemos.Contracts.ICalculate"
  17:                     listenUriMode="Unique" />
  18:                <!--3. NetTcpBinding & ListenUriMode.Explicit-->
  19:                 <endpoint address="net.tcp://127.0.0.1:7777/service3" binding="netTcpBinding"
  20:                     bindingConfiguration="" name="tcpExplicitListenUriMode" contract="Artech.ListenUriDemos.Contracts.ICalculate" />
  21:                 <!--4. NetTcpBinding & ListenUriMode.Unique-->
  22:                 <endpoint address="net.tcp://127.0.0.1:8888/service4" binding="netTcpBinding"
  23:                     bindingConfiguration="" name="tcpUniquListenUriMode" contract="Artech.ListenUriDemos.Contracts.ICalculate"
  24:                     listenUriMode="Unique" />
  25:                 <!--5. NetTcpBinding & ListenUriMode.Unique & Port Sharing-->
  26:                 <endpoint address="net.tcp://127.0.0.1:9999/service5" binding="netTcpBinding"
  27:                     bindingConfiguration="PortSharingBinding" name="tcpPortSharingUniquListenUriMode"
  28:                     contract="Artech.ListenUriDemos.Contracts.ICalculate" listenUriMode="Unique" />
  29:             </service>
  30:         </services>
  31:     </system.serviceModel>    
  32: </configuration> 
  33:  

   1: //---------------------------------------------------------------
   2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
   3: //---------------------------------------------------------------
   4: using System;
   5: using System.ServiceModel;
   6: using Artech.ListenUriDemos.Services;
   7: using System.ServiceModel.Dispatcher; 
   8:  
   9: namespace Artech.ListenUriDemos.Hosting
  10: {
  11:     class Program
  12:     {
  13:         static void Main(string[] args)
  14:         {
  15:             using (ServiceHost serviceHost = new ServiceHost(typeof(CalculateService)))
  16:             {
  17:                 serviceHost.Open(); 
  18:  
  19:                 int i = 0;
  20:                 foreach (ChannelDispatcher channelDispatcher in serviceHost.ChannelDispatchers)
  21:                 {
  22:                     Console.WriteLine("第{0}個終結點的監聽地址為: {1}", ++i,channelDispatcher.Listener.Uri);
  23:                 } 
  24:  
  25:                 Console.Read();
  26:             }
  27:         }
  28:     }
  29: } 
  30:  

   1: 第1個終結點的監聽地址為: https://127.0.0.1:5555/service1
   2: 第2個終結點的監聽地址為: https://127.0.0.1:6666/service2/d9ce6f30-3103-4ec9-b73b-34f32c65b0a1
   3: 第3個終結點的監聽地址為: net.tcp://127.0.0.1:7777/service3
   4: 第4個終結點的監聽地址為: net.tcp://127.0.0.1:1119/service4
   5: 第5個終結點的監聽地址為: net.tcp://127.0.0.1:9999/service5/b4f69288-913b-43ec-8e42-e58f150ee91c

 

WCF後續之旅: 
WCF後續之旅(1): WCF是如何通過Binding進行通信的 
WCF後續之旅(2): 如何對Channel Layer進行擴展——創建自定義Channel 
WCF後續之旅(3): WCF Service Mode Layer 的中樞—Dispatcher 
WCF後續之旅(4):WCF Extension Point 概覽 
WCF後續之旅(5): 通過WCF Extension實現Localization 
WCF後續之旅(6): 通過WCF Extension實現Context信息的傳遞 
WCF後續之旅(7):通過WCF Extension實現和Enterprise Library Unity Container的集成 
WCF後續之旅(8):通過WCF Extension 實現與MS Enterprise Library Policy Injection Application Block 的集成 
WCF後續之旅(9):通過WCF的雙向通信實現Session管理[Part I] 
WCF後續之旅(9): 通過WCF雙向通信實現Session管理[Part II] 
WCF後續之旅(10): 通過WCF Extension實現以對象池的方式創建Service Instance 
WCF後續之旅(11): 關於並發、回調的線程關聯性(Thread Affinity) 
WCF後續之旅(12): 線程關聯性(Thread Affinity)對WCF並發訪問的影響 
WCF後續之旅(13): 創建一個簡單的WCF SOAP Message攔截、轉發工具[上篇] 
WCF後續之旅(13):創建一個簡單的SOAP Message攔截、轉發工具[下篇] 
WCF後續之旅(14):TCP端口共享 
WCF後續之旅(15): 邏輯地址和物理地址 
WCF後續之旅(16): 消息是如何分發到Endpoint的--消息篩選(Message Filter) 
WCF後續之旅(17):通過tcpTracer進行消息的路由



作者:蔣金楠
微信公眾賬號:大內老A
微博:www.weibo.com/artech
如果你想及時得到個人撰寫文章以及著作的消息推送,或者想看看個人推薦的技術資料,可以掃描左邊二維碼(或者長按識別二維碼)關注個人公眾號(原來公眾帳號蔣金楠的自媒體將會停用)。
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁麵明顯位置給出原文連接,否則保留追究法律責任的權利。
原文鏈接

最後更新:2017-10-30 16:34:26

  上一篇:go  三步教你搭建給黑白照片上色的神經網絡 !(附代碼)
  下一篇:go  WCF後續之旅(17):通過tcpTracer進行消息的路由