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="data source=.\SQLEXPRESS;initial catalog=Test;integrated security=SSPI;
MultipleActiveResultSets=True;App=EntityFramework"" 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>
相关文章
- 计算机的运行原理
- 操作系统高频面试题(2022最新整理)
- 利用宝塔面板搭建nginx及PhpMyadmin等环境
- 一键卸载宝塔Linux面板及运行环境命令
- 微信小程序Video组件实现视频、自定义播放按钮、封面图、封面图文字demo
- 微信小程序页面跳转传值
- Win10家庭版使用远程桌面连接失败
- 耶鲁两栖机器龟登Nature封面,一种部件搞定水上、陆地行动
- Crane 发布国内首个云原生应用碳排放计算优化器
- 7 Papers & Radios | 两栖机器龟登上Nature封面;深度去模糊综述论文入选IJCV
- SIGGRAPH Asia 2022|告别手Key!上科大提出实时面捕,精细到微表情
- 图灵奖得主Jack Dongarra:超级计算顶部还有很大空间
- 云图说丨手把手教你为容器应用配置弹性伸缩策略
- Spark优化之小文件是否需要合并?
- 如何让“哑”终端进化,你知道吗?
- Web前端性能优化,应该怎么做?
- 看DLI服务4核心如何提升云服务自动化运维
- 5G从小就梦想着自己要迎娶:高速率、低时延、大容量三个老婆
- 一文读懂GaussDB(for Mongo)的计算存储分离架构
- 9个常用ES6特性归纳(一般用这些就够了)