WCF学习之旅—TcpTrace工具(二十六)

止文(WCF学习之旅—TcpTrace工具(二十五))介绍了关于TcpTrance的一种使用方式,接下来介绍第二种使用方式。

三、通过ListenUri实现基于tcpTracer的消息路由

       对于路由的实现,本质上就是实现逻辑地址和物理地址的分离。通过前面的示例介绍,我们了解,如何在客户端实现TcpTracer捕获客户端与服务端的通信信息。在这一章节中我们学习如能通过设置ListenUri实现基于服务端的TcpTracer的消息路由。

      服务端的物理地址和逻辑地址的分离通过ListenUri实现,ListenUri指定了服务的物理地址,大家可能觉得奇怪,我们设置服务地址的时候一般都是设 置Address(ABC的一部分),但是Address指定的是逻辑地址,而ListenUri指定的是物理地址,如果指定了这个属性的值,那么就指定了服务的物理地址。

       通过ListenUri的实现的基本原理如下图所示:客户端保持不变,在对服务进行寄宿的时候,将 ListenUri的端口设为8080,那么服务实际的监听地址的端口将从8888变成8080。由于客户端保持不变,所以请求消息仍然发送到端口 8888,为了实现TcpTracer对消息正常的路由,只需要将原端口和目的端口指定为8888(逻辑地址)和8080(物理地址)就可以了。 如下图。

 

      为此,我们需要修改服务寄宿的配置,在终结点配置节中指定listenUri为http://127.0.0.1:8080/BookService

<?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> <bindings> <customBinding> <binding name="listenUriBinding"> <textMessageEncoding /> <httpTransport /> </binding> </customBinding> </bindings> <services> <service name="SCF.WcfService.BookService"> <endpoint address="http://127.0.0.1:8888/BookService" binding="customBinding" bindingConfiguration="listenUriBinding" listenUri="http://127.0.0.1:8080/BookService" contract="SCF.Contracts.IBookService" /> </service> </services> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <connectionStrings> <add name="Entities" connectionString="metadata=res://*/BookModel.csdl|res://*/BookModel.ssdl|res://*/BookModel.msl;
provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;initial catalog=Test;integrated security=SSPI;
MultipleActiveResultSets=True;App=EntityFramework&quot;"
providerName="System.Data.EntityClient" /> </connectionStrings> </configuration>

     现在我们启动tcpTracer,将Listen On Port#和Destination Port #设置为8888和8080。 如下图。

 

     当我们先后启动服务寄宿和服务访问的控制台应用程序,在tcpTracer中,我们可以得到和步骤二一样的结果。如下图。

 

 

注意,在实际的使用过程中,出现过以下问题。

 

WCF 异常:无法打开安全通道,因为与远程终结点的安全协商已失败。这可能是由于用于创建通道的 EndpointAddress 中不存在 EndpointIdentity

   遇到这个问题我的第一反应就是客户端和服务端的配置不同造成的。

    如果是通过VS引用服务地址的方式添加的服务只要更新服务引用后这个问题就解决了。

   但是如果是通过自己手写代码调用或是使用配置文件的就要检查客户端与服务端的代码与配置文件中的信息。

<?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>
          <customBinding>
            <binding name="listenUriBinding">
              <textMessageEncoding />
              <httpTransport />
            </binding>
          </customBinding>
        </bindings>
        <client>

            <endpoint address="http://127.0.0.1:8888/BookService" binding="customBinding" bindingConfiguration="listenUriBinding" 
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>

 

posted @ 2016-09-29 15:41  DotNet菜园  阅读(2818)  评论(1编辑  收藏  举报