我如何获得一个WCF Web服务请求的XML SOAP请求?如何获得、WCF、Web、XML

2023-09-02 10:42:41 作者:一天一天跌进我的心

我打电话给在code这个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在客户端,而不是在服务器跟踪它。在这种情况下,你的答案就在我上面链接的问题,以及在如何检查或修改信息客户。但是,由于.NET 4的版本该文章的缺失其C#和.NET 3.5的例子有一些混乱(如果不是bug)吧,这扩大了你的目的。

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咸的回答的另一个问题,寻找这些链接。

Thanks to Zach Bonham's answer at another question for finding these links.