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


WCF後續之旅(17):通過tcpTracer進行消息的路由

image

服務契約:Artech.TcpTraceDemo.Contracts.ICalculate

   1: //---------------------------------------------------------------
   2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
   3: //---------------------------------------------------------------
   4: using System.ServiceModel;
   5: namespace Artech.TcpTraceDemo.Contracts
   6: {
   7:     [ServiceContract]
   8:     public interface ICalculate
   9:     {
  10:         [OperationContract]
  11:         double Add(double x, double y);
  12:     }
  13: } 

服務實現:Artech.TcpTraceDemo. Services. CalculateService

   1: //---------------------------------------------------------------
   2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
   3: //---------------------------------------------------------------
   4: using Artech.TcpTraceDemo.Contracts; 
   5:  
   6: namespace Artech.TcpTraceDemo.Services
   7: {
   8:     public class CalculateService:ICalculate
   9:     {
  10:         #region ICalculate Members 
  11:  
  12:         public double Add(double x, double y)
  13:         {
  14:             return x + y;
  15:         } 
  16:  
  17:         #endregion
  18:     }
  19: } 

服務寄宿(代碼):Artech.TcpTraceDemo.Hosting. Program

   1: //---------------------------------------------------------------
   2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
   3: //---------------------------------------------------------------
   4: using System;
   5: using System.ServiceModel;
   6: using Artech.TcpTraceDemo.Services; 
   7:  
   8: namespace Artech.TcpTraceDemo.Hosting
   9: {
  10:     class Program
  11:     {
  12:         static void Main(string[] args)
  13:         {
  14:             using (ServiceHost serviceHost = new ServiceHost(typeof(CalculateService)))
  15:             {
  16:                 serviceHost.Opened += delegate
  17:                 {
  18:                     Console.WriteLine("The Calculate Service has been started up!");
  19:                 };
  20:                 serviceHost.Open(); 
  21:  
  22:                 Console.Read();
  23:             }
  24:         }
  25:     }
  26: } 
  27:  

服務寄宿(配置):App.config

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:     <system.serviceModel>
   4:         <bindings>
   5:             <customBinding>
   6:                 <binding name="SimpleBinding">
   7:                     <textMessageEncoding />
   8:                     <httpTransport />
   9:                 </binding>
  10:             </customBinding>
  11:         </bindings>
  12:         <services>
  13:             <service name="Artech.TcpTraceDemo.Services.CalculateService">
  14:                 <endpoint address="https://127.0.0.1:9999/calculateservice" binding="customBinding"                    bindingConfiguration="SimpleBinding" contract="Artech.TcpTraceDemo.Contracts.ICalculate"/>
  15:             </service>
  16:         </services>
  17:     </system.serviceModel>
  18: </configuration> 
  19:  

注:由於本例僅僅用於模擬消息的路由,所以我們僅僅需要綁定提供的傳輸和編碼功能,所以在這裏我使用了自定義綁定,並且添加兩個BindElement:HttpTransport和TextMessageEncoding。

服務訪問(代碼):Artech.TcpTraceDemo.Clients.Program

   1: //---------------------------------------------------------------
   2: // EndpointAddress & WCF Addressing (c) by 2008 Jiang Jin Nan
   3: //---------------------------------------------------------------
   4: using System.ServiceModel;
   5: using Artech.TcpTraceDemo.Contracts;
   6: using System;
   7: namespace Artech.TcpTraceDemo.Clients
   8: {
   9:     class Program
  10:     {
  11:         static void Main(string[] args)
  12:         {
  13:             using (ChannelFactory<ICalculate> channelFactory = new ChannelFactory<ICalculate>("calculateService"))
  14:             {
  15:                 ICalculate calculator = channelFactory.CreateChannel();
  16:                 using (calculator as IDisposable)
  17:                 { 
  18:                     Console.WriteLine("x + y = {2} where x = {0} and y = {1}",1,2,calculator.Add(1,2));
  19:                 }
  20:             } 
  21:  
  22:             Console.Read();
  23:         }
  24:     }
  25: } 
  26:  

服務訪問(配置):App.config

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:     <system.serviceModel>        
   4:         <bindings>
   5:             <customBinding>
   6:                 <binding name="SimpleBinding">
   7:                     <textMessageEncoding />
   8:                     <httpTransport />
   9:                 </binding>
  10:             </customBinding>
  11:         </bindings>
  12:         <client>
  13:             <endpoint  address="https://127.0.0.1:9999/calculateservice"
  14:                 binding="customBinding" bindingConfiguration="SimpleBinding"
  15:                 contract="Artech.TcpTraceDemo.Contracts.ICalculate" name="calculateService" />
  16:         </client>
  17:     </system.serviceModel>
  18: </configuration> 
  19:  

步驟二、通過ClientViaBehavior實現基於tcpTracer的消息路由

tcpTracer.ClientVia

注:對於消息發送方來說,SOAP消息的To報頭對應的地址由發送端的終結點地址(邏輯地址)決定。

   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:         <bindings>
  12:             <customBinding>
  13:                 <binding name="SimpleBinding">
  14:                     <textMessageEncoding />
  15:                     <httpTransport />
  16:                 </binding>
  17:             </customBinding>
  18:         </bindings>
  19:         <client>
  20:             <endpoint  address="https://127.0.0.1:9999/calculateservice" behaviorConfiguration="clientViaBehavior"
  21:                 binding="customBinding" bindingConfiguration="SimpleBinding"
  22:                 contract="Artech.TcpTraceDemo.Contracts.ICalculate" name="calculateService" />
  23:         </client>
  24:     </system.serviceModel>
  25: </configuration> 
  26:  

image

image

   1: POST /calculateservice HTTP/1.1
   2: Content-Type: application/soap+xml; charset=utf-8
   3: VsDebuggerCausalityData: uIDPo2sY41w6xm1DgtOSzZT5+0EAAAAAXVfsUhiXVUmLsNq6tAEl+rUZZUmtRERFvB6DbqcWQtcACQAA
   4: Host: 127.0.0.1:8888
   5: Content-Length: 526
   6: Expect: 100-continue
   7: Connection: Keep-Alive 
   8:  
   9: <s:Envelope xmlns:s="https://www.w3.org/2003/05/soap-envelope" xmlns:a="https://www.w3.org/2005/08/addressing">
  10:     <s:Header>
  11:         <a:Action s:mustUnderstand="1">https://tempuri.org/ICalculate/Add</a:Action>
  12:         <a:MessageID>urn:uuid:a63ec626-a350-4390-84c6-fb34be4ff208</a:MessageID>
  13:         <a:ReplyTo>            
  14:                 <a:Address>https://www.w3.org/2005/08/addressing/anonymous</a:Address>
  15:         </a:ReplyTo>
  16:         <a:To s:mustUnderstand="1">https://127.0.0.1:9999/calculateservice</a:To>
  17:     </s:Header>
  18:     <s:Body>
  19:         <Add xmlns="https://tempuri.org/">
  20:             <x>1</x>
  21:             <y>2</y>
  22:         </Add>
  23:     </s:Body>
  24: </s:Envelope> 
  25:  

   1: HTTP/1.1 100 Continue 
   2:  
   3: HTTP/1.1 200 OK
   4: Content-Length: 394
   5: Content-Type: application/soap+xml; charset=utf-8
   6: Server: Microsoft-HTTPAPI/2.0
   7: Date: Sat, 13 Sep 2008 17:29:37 GMT 
   8:  
   9: <s:Envelope xmlns:s="https://www.w3.org/2003/05/soap-envelope" xmlns:a="https://www.w3.org/2005/08/addressing">
  10:     <s:Header>
  11:         <a:Action s:mustUnderstand="1">https://tempuri.org/ICalculate/AddResponse</a:Action>
  12:         <a:RelatesTo>urn:uuid:a63ec626-a350-4390-84c6-fb34be4ff208</a:RelatesTo>
  13:     </s:Header>
  14:     <s:Body>
  15:         <AddResponse xmlns="https://tempuri.org/">
  16:             <AddResult>3</AddResult>
  17:         </AddResponse>
  18:     </s:Body>
  19: </s:Envelope> 
  20:  

步驟三、通過ListenUri實現基於tcpTracer的消息路由

image

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:     <system.serviceModel>
   4:         <bindings>
   5:             <customBinding>
   6:                 <binding name="SimpleBinding">
   7:                     <textMessageEncoding />
   8:                     <httpTransport />
   9:                 </binding>
  10:             </customBinding>
  11:         </bindings>
  12:         <services>
  13:             <service name="Artech.TcpTraceDemo.Services.CalculateService">
  14:                 <endpoint address="https://127.0.0.1:9999/calculateservice" binding="customBinding"
  15:                     bindingConfiguration="SimpleBinding" contract="Artech.TcpTraceDemo.Contracts.ICalculate"
  16:                     listenUri="https://127.0.0.1:8888/calculateservice" />
  17:             </service>
  18:         </services>
  19:     </system.serviceModel>
  20: </configuration> 
  21:  

image

 

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

  上一篇:go  WCF後續之旅(15): 邏輯地址和物理地址
  下一篇:go  WCF中的Binding模型之一: Binding模型簡介