在 WCF 客户端中拦截消息 [英] Intercept messages in a WCF Client

查看:24
本文介绍了在 WCF 客户端中拦截消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人对 Web 服务扩展有任何经验吗?我花了一些时间尝试根据 MS 示例制作 Web 服务扩展.

Has anyone got any experience with Web Service Extensions? I spent time trying to make a web service extension from the MS examples.

我有一个 .net 3.5 Web 服务客户端,它是通过 VS IDE项目 > 添加服务引用"添加对 WSDL 的引用构建的.这构建了我的 Web 服务客户端,一切正常.

I have an .net 3.5 web service client, built by adding a reference to the WSDL, via the VS IDE "Project > Add Service Reference". This built my web service client, and all works OK.

我需要拦截 Web 服务客户端的请求和响应正文.我发现了很多对 Web 服务扩展的引用,但我感到很累,无法启动我的扩展.

I need to intercept the request and response body for my web service client. I have found lots of references to Web Service Extensions, but am having an attack of the tired, and just can't get my extensions to fire.

我使用了此处的 MS 示例如何实现 SOAP 扩展"(http://msdn.microsoft.com/en-us/library/7w06t139.aspx) ,它为请求/响应流构建一个记录器.

I've used the MS example from here "How to implement a SOAP extension" ( http://msdn.microsoft.com/en-us/library/7w06t139.aspx) , which builds a logger for the request / response streams.

相关的 MS 文章Soap Message Modification"(http://msdn.microsoft.com/en-us/library/esw638yk(VS.85).aspx) 显示了如何为 Web 客户端启用 SOAP 扩展:

The related MS article "Soap Message Modification" (http://msdn.microsoft.com/en-us/library/esw638yk(VS.85).aspx) shows how to enable the SOAP extension for the web client:

实现 SOAP 扩展

有两种方法可以在客户端或服务器应用程序上运行 SOAP 扩展.首先,您可以配置应用程序以运行扩展.要将 SOAP 扩展配置为针对所有 Web 服务(尤其是 vroot)上的所有 Web 方法运行,请编辑 Web.config 文件中的 元素部分.以下代码显示 type 属性值必须在一行中,并包含扩展的完全限定名称,以及已签名程序集的版本、区域性和公钥标记.

There are two ways to run a SOAP extension on either a client or server application. First, you can configure the application to run the extension. To configure your SOAP extension to run for all Web methods on all Web services, especially a vroot, edit the <soapExtensionTypes> Element section within the Web.config file. The following code shows that the type attribute value must be on one line and include the fully qualified name of the extension, plus the version, culture, and public key token of the signed assembly.

<配置>
<system.web>
<网络服务>
<soapExtensionTypes>
<add type="Contoso.MySoapExtension, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"priority="1" group="0"/>
</soapExtensionTypes>
</webServices>
</system.web>
</配置>

<configuration>
<system.web>
<webServices>
<soapExtensionTypes>
<add type="Contoso.MySoapExtension, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"/>
</soapExtensionTypes>
</webServices>
</system.web>
</configuration>

我已经将 traceextension 编译成它自己的类库,并在 web 服务项目的 web.config 中像这样引用它:

I've compiled the traceextension into its own class library, and referenced it in the web.config of the web service project like so:

<add type="TraceExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef8757fac167b8d8" priority="1" group="High"/>

<add type="TraceExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef8757fac167b8d8" priority="1" group="High"/>

没有快乐.未记录任何内容,未命中任何断点.

No joy. Nothing is logged, no breakpoints are hit.

然后我删除了引用的类,并将源代码放入 Web 服务项目中.

I then removed the referenced class, and dropped the source code into the web service project.

我尝试像这样添加对它的引用(我的命名空间是 ServcieTest001):

I tried to add a reference to it like so (my namespace is ServcieTest001):

<add type="ServiceTest001.TraceExtension" group="High" priority="1"/>

<add type="ServiceTest001.TraceExtension" group="High" priority="1" />

我使用以下线程作为指导,使我能够扩展getting-raw-soap-data-from-a-web-reference-client-running-in-asp-net"(http://stackoverflow.com/questions/300674/Getting-raw-soap-data-from-a-web-reference-client-running-in-asp-net).

I used the following thread as a guide as to enabling me extension "getting-raw-soap-data-from-a-web-reference-client-running-in-asp-net" (http://stackoverflow.com/questions/300674/getting-raw-soap-data-from-a-web-reference-client-running-in-asp-net).

仍然没有快乐.然后我从上面的线程中复制了代码,但在发出 SOAP 请求时仍然无法触发扩展.

Still no joy. I then copied the code from the above thread, and still cannot get the extension to fire when I make a SOAP request.

谁能告诉我一个功能强大的可下载 Web 服务扩展演示项目,以便我可以分解它并找出我缺少的东西?

Can anyone point me to a functioning downloadable web service extension demo project, so I can disassemble it and work out what I'm missing?

推荐答案

John 是对的,您可以使用实现 IClientMessageInspector 的自定义客户端行为拦截客户端上的消息.请参阅 MSDN 上的如何:检查或修改客户端上的消息.

John is right, you can intercept the messages on the client using a custom client behavior that implements IClientMessageInspector. See How To: Inspect or Modify Messages on the Client on MSDN.

唯一棘手"的是,如果您计划修改消息正文,那么您需要先创建原始消息的副本.请参阅使用消息类了解相关详细信息.

The only thing 'tricky' about it is that if you plan on modifying the message body then you will need to create a copy of the original message first. See Using the Message Class for the gooey details.

这篇关于在 WCF 客户端中拦截消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆