如何获取 WCF Web 服务请求的 XML SOAP 请求? [英] How do I get the XML SOAP request of an WCF Web service request?
问题描述
我在代码中调用此 Web 服务,我想查看 XML,但找不到公开它的属性.
I'm calling this web service within code and I would like to see the XML, but I can't find a property that exposes it.
推荐答案
我认为您的意思是您希望在客户端查看 XML,而不是在服务器上跟踪它.在这种情况下,您的答案在我上面链接的问题中,也在 How to检查或修改客户端上的消息.但是,由于那篇文章的 .NET 4 版本缺少它的 C#,并且 .NET 3.5 示例中存在一些混淆(如果不是错误),因此在这里对其进行了扩展.
I think you meant that you want to see the XML at the client, not trace it at the server. In that case, your answer is in the question I linked above, and also at How to Inspect or Modify Messages on the Client. But, since the .NET 4 version of that article is missing its C#, and the .NET 3.5 example has some confusion (if not a bug) in it, here it is expanded for your purpose.
您可以使用 IClientMessageInspector:
using System.ServiceModel.Dispatcher;
public class MyMessageInspector : IClientMessageInspector
{ }
该接口中的方法 BeforeSendRequest
和 AfterReceiveReply
允许您访问请求和回复.要使用检查器,您需要将其添加到 IEndpointBehavior:
The methods in that interface, BeforeSendRequest
and AfterReceiveReply
, give you access to the request and reply. To use the inspector, you need to add it to an IEndpointBehavior:
using System.ServiceModel.Description;
public class InspectorBehavior : IEndpointBehavior
{
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new MyMessageInspector());
}
}
您可以将该接口的其他方法保留为空实现,除非您也想使用它们的功能.阅读操作方法了解更多详情.
You can leave the other methods of that interface as empty implementations, unless you want to use their functionality, too. Read the how-to for more details.
在您实例化客户端后,将行为添加到端点.使用示例 WCF 项目中的默认名称:
After you instantiate the client, add the behavior to the endpoint. Using default names from the sample WCF project:
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.Endpoint.Behaviors.Add(new InspectorBehavior());
client.GetData(123);
在MyMessageInspector.BeforeSendRequest()
中设置断点;request.ToString()
被重载以显示 XML.
Set a breakpoint in MyMessageInspector.BeforeSendRequest()
; request.ToString()
is overloaded to show the XML.
如果您要操纵消息,则必须处理消息的副本.有关详细信息,请参阅使用消息类.
If you are going to manipulate the messages at all, you have to work on a copy of the message. See Using the Message Class for details.
感谢 Zach Bonham 在另一个问题中的回答找到这些链接.
这篇关于如何获取 WCF Web 服务请求的 XML SOAP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!