WCF學習之旅—TcpTrace工具(二十五)

      前面的幾篇文章,咱們學習了怎麼開發WCF應用程序與服務,也學習瞭如何進行WCF的配置。對於Web Service與WCF服務應用,服務端與客戶端的通訊是經過收發SOAP Message進行,咱們如何有效而快速的獲取通訊信息呢?這就是本文要介紹的一個工具。html

1、TcpTrace實現的基本原理

      在對Web Service和WCF進行調試時,可使用Soap Trace 工具對Soap Message進行深刻捕獲並進行分析。常常使用的工具備TcpTrace與Microsoft Soap Toolkit中的Soap Trace Utility。網絡

      對於但願對WCF的消息交換有一個深層次瞭解的開發者來講,TcpTracer絕對是一個不可多得好工具。咱們將TcpTracer置於服務和服務代理之間,TcpTracer會幫助咱們接獲、顯示和轉發流經他的消息。tcp

      首先來說講TcpTrace實現的基本原理。說簡單點,TcpTracer就是一個監聽/轉發器(Listening/Forwarding),就是一個路由器。當啓動的時候,咱們須要設置兩個端口:監聽端口(Listening Port)和目的主機(Destination Server)與目的端口(Destination Port),而後TcpTracer就會在本機的監聽端口進行網絡監聽。一旦有針對該監聽端口的請求抵達,他會截獲整個請求的消息,並將整個消息顯示到消息面板上。隨後,TcpTracer會將該消息原封不動地轉發給目的主機與目的端口。在另外一方面,從目的主機與目的端口發送給原端口的消息,也一樣被TcpTracer截獲、顯示和轉發。 說白了就是把要發的消息先給咱們查看和備份,再轉發出去。ide

      接下來咱們咱們經過下面的步驟演示如何經過TcpTracer在WCF中進行消息的路由。工具

      1) 爲了演示TcpTracer在WCF中的應用,仍是用咱們前面作的書籍查詢示例的WCF服務應用(BookService),具體參見WCF學習之旅——第一個WCF示例(一)WCF學習之旅—WCF第二個示例(五)學習

       2) 示例中的終結點的地址爲:http://127.0.0.1:8888/BookService(Port爲8888)。ui

       3) 同時這個服務端的配置文件信息以下:spa

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework
, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" /> </configSections> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> <system.serviceModel> <diagnostics> <messageLogging logEntireMessage="true" logKnownPii="false" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" /> <endToEndTracing propagateActivity="true" activityTracing="true" messageFlowTracing="true" /> </diagnostics> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8888/BookService/metadata" /> <serviceDebug includeExceptionDetailInFaults="True" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="SCF.WcfService.BookService"> <endpoint address="http://127.0.0.1:8888/BookService" binding="wsHttpBinding" contract="SCF.Contracts.IBookService" /> </service> </services> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true"> <listeners> <add name="xml" /> </listeners> </source> </sources> <sharedListeners> <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="D:\wcf.svclog" /> </sharedListeners> </system.diagnostics> </configuration>

 

    4) 客戶端的配置信息以下:3d

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IBookService" />
                <binding name="WSHttpBinding_IBookService1" />
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://127.0.0.1:8888/BookService" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IBookService" contract="SCF.Contracts.IBookService"
                name="WSHttpBinding_IBookService">
                <identity>
                    <userPrincipalName value="DEVELOPER\Administrator" />

                </identity>
            </endpoint>
            <endpoint address="http://127.0.0.1:8888/BookService" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IBookService1" contract="BookServiceRef.IBookService"

                name="WSHttpBinding_IBookService1">
                <identity>
                    <userPrincipalName value="DEVELOPER\Administrator" />

                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

 

      5)  可是經過上面的設置以後,實際上仍是不可以進行數據的捕捉,以下圖,不管如何在客戶端調用WCF應用,TcpTrace是沒法捕捉到任何數據的。代理

 

   這是爲何呢?請往下看。

2、經過ClientViaBehavior實現基於TcpTracer的消息路由

      在咱們建立的WCF服務來講,整個服務訪問只涉及到兩方:服務(BookService)和服務的調用者(客戶端)。從消息交換的角度來看,服務的調用者調用者將請求消息直接發送到服務端,計算結果也以回覆消息的形式直接返回到服務的調用者。

      如今咱們須要將TcpTracer做爲一個路由器引入到服務(BookService)和服務的調用者 (客戶端)之間,那麼咱們須要解決的是:服務調用者發送的消息不能直接發送到服務端,而應該先發送給TcpTracer,再由TcpTracer轉發給服務。咱們能夠經過ClientViaBehavior實現邏輯地址和物理地址的分離——邏輯地址指向最終的服務,而物理地址則指向 TcpTracer。

      具體的原理以下圖所示:咱們將TcpTracer的監聽端口(Listening Port)和目的端口(Destination port)設置成8080和8888(BookService地址所在的端口)。經過ClientViaBehavior將物理地址的端口設成 8080(TcpTracer監聽端口)。

 

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

      基於上面的實現原理,咱們須要修改客戶端的配置, 在<system.serviceModel>/<behaviors>/<endpointBehaviors> 添加ClientViaBehavior,將viaUri的端口指定爲8080:http://127.0.0.1:8080/BookService。並將該EndpointBehavior應用到終結點中。

      1)  配置文件信息修改一下,以下面。請仔細比較一下面的客戶端配置文件與上篇文章最後的客戶端配置文件的區別:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>

    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IBookService" />
                <binding name="WSHttpBinding_IBookService1" />

            </wsHttpBinding>
        </bindings>
          <behaviors>
             <endpointBehaviors>
                 <behavior name="bookServiceEndpointBehavior">
                     <clientVia viaUri="http://localhost:8080/BookService" />

                 </behavior>
             </endpointBehaviors>
         </behaviors>
        <client>
            <endpoint address="http://127.0.0.1:8888/BookService" binding="wsHttpBinding" behaviorConfiguration="bookServiceEndpointBehavior"
                bindingConfiguration="WSHttpBinding_IBookService" contract="SCF.Contracts.IBookService"
                name="WSHttpBinding_IBookService">
                <identity>
                    <userPrincipalName value="DEVELOPER\Administrator" />

                </identity>
            </endpoint>
            <endpoint address="http://127.0.0.1:8888/BookService" binding="wsHttpBinding" behaviorConfiguration="bookServiceEndpointBehavior"
                bindingConfiguration="WSHttpBinding_IBookService1" contract="BookServiceRef.IBookService"
                name="WSHttpBinding_IBookService1">
                <identity>
                    <userPrincipalName value="DEVELOPER\Administrator" />

                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

 

      2) 通過修改保存以後,如今能夠進行Soap Trace了,如今咱們再次啓動TcpTrace。進行以下的設置,以下圖。

     監聽端口(Listen On Port#):能夠自由設置,可是不可以與服務的端口重合,而且該端口還須要在客戶端註冊。在這裏設置成8080

     目標端口(Destination Port #):服務端口。在這裏設置成8888。

 

    3) 先運行Hosting程序,而後運行WinClient,在WinClient中發起請求,這時請求消息和回覆消息將會顯示到TcpTracer的消息顯示面板中,以下圖所示:

 

     4)  咱們還能夠經過日誌,把信息記錄下來,記錄日誌的配置以下圖。

 

     5)  當關閉tcpTrace時相應的內容被記錄到咱們指定的Log文件中:

 

相關文章
相關標籤/搜索