C# 在 WCF 客户端中拦截消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1217374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:46:57  来源:igfitidea点击:

Intercept messages in a WCF Client

c#wcfsoapwcf-client

提问by

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

有没有人对 Web 服务扩展有任何经验?我花了一些时间尝试从 MS 示例中制作 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.

我有一个 .net 3.5 Web 服务客户端,它是通过 VS IDE“项目 > 添加服务引用”添加对 WSDL 的引用构建的。这构建了我的 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.

我需要拦截 Web 服务客户端的请求和响应正文。我找到了很多对 Web Service Extensions 的引用,但是我已经厌倦了,并且无法启动我的扩展。

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.

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

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:

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

Implementing the SOAP Extension

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.

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

实现 SOAP 扩展

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

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

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

我已经将 traceextension 编译到它自己的类库中,并在 web 服务项目的 web.config 中引用它,如下所示:

<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.

没有喜悦。什么都没有记录,没有断点被击中。

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

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

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

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

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

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

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).

我使用以下线程作为指导,使我能够扩展“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)。

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.

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

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?

任何人都可以指点我一个功能强大的可下载 Web 服务扩展演示项目,这样我就可以反汇编它并找出我缺少的东西吗?

采纳答案by Zach Bonham

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 Clienton MSDN.

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

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

唯一“棘手”的是,如果您计划修改邮件正文,则需要先创建原始邮件的副本。有关粘糊糊的详细信息,请参阅使用消息类

回答by John Saunders

Chances are you want to get some rest.

很有可能你想休息一下。

You don't ever want to use WSE. WSE is obsolete.

您永远不想使用 WSE。WSE 已经过时了。

You don't want to be using ASMX Web Services - Microsoft now considers them to be "legacy" technology, and will not be fixing bugs. BTW, WSE is based on ASMX, so what's that make it?

您不想使用 ASMX Web 服务 - 微软现在认为它们是“遗留”技术,不会修复错误。顺便说一句,WSE 是基于 ASMX 的,那是什么?



You only want to work with Windows Communication Foundation. The WCF Development Center on MSDN is at http://msdn.microsoft.com/wcf/.

您只想使用 Windows Communication Foundation。MSDN 上的 WCF 开发中心位于http://msdn.microsoft.com/wcf/

Have fun, and stay away from the nasty, ancient, obsolete stuff.

玩得开心,远离讨厌的、古老的、过时的东西。

回答by John Saunders

Yup - I was too tired.

是的 - 我太累了。

I thinkthe Web Service Extensions work with "Web references" (.net 2), not "Service references" (.net 3).

认为Web 服务扩展适用于“Web 引用”(.net 2),而不是“服务引用”(.net 3)。

So I guess to alter my question - how do I intercept the request and response for a .net 3 "Service reference" connected to a legacy web service?

所以我想改变我的问题 - 如何拦截连接到旧 Web 服务的 .net 3“服务引用”的请求和响应?