简体   繁体   English

如何在c#中查看对服务引用的传入响应的完整SOAP响应(包括标头)?

[英]How do I view the full SOAP response (including header) on incoming responses to a Service Reference in c#?

I have a simple c# 3.5 .Net console application that hooks up to a service reference. 我有一个简单的c#3.5 .Net控制台应用程序,它连接到服务引用。 Everything's working fine - making calls and receiving responses, but now I've been told to look at the Soap header in the message that's coming back. 一切正常 - 打电话和接收回复,但现在我被告知要查看消息中的Soap标题。

I've found the .Net WebService Studio which is pretty awesome and will show both the Soap request and the Soap response. 我发现.Net WebService Studio非常棒,并且会显示Soap请求和Soap响应。

For a response, it shows something like this: 对于响应,它显示如下:

ResponseCode: 200 (OK)
Content-Language:en-US
Content-Length:30048
Content-Type:text/xml; charset=utf-8
Date:Mon, 25 Jan 2010 19:57:47 GMT
Server:WebSphere Application Server/6.1

<?xml version="1.0" encoding="utf-16"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header />
  <soapenv:Body>

How can I generate something similar in my application? 如何在我的应用程序中生成类似的东西?

The response I'm interested in looking at is for a different method that returns a large enough message to blow up the WebService Studio. 我感兴趣的响应是针对一种不同的方法,它返回足够大的消息来炸毁WebService Studio。 I don't see how to set message size parameters using this tool. 我没有看到如何使用此工具设置邮件大小参数。 So, I want to just capture this information myself. 所以,我想自己捕获这些信息。

Any ideas on how I can do this? 有关如何做到这一点的任何想法?

WCF has tracing via the config file , or you can implement a behavior to log the message yourself. WCF通过配置文件进行跟踪,或者您可以实现自己记录消息的行为。

Add the behavior like this: 添加如下行为:

Service1SoapClient client = new Service1SoapClient();
client.Endpoint.Behaviors.Add( new MessageInspectionBehavior());
client.HelloWorld();

and the code: 和代码:

class MessageInspectionBehavior : IClientMessageInspector, IEndpointBehavior
{
    public void Validate(ServiceEndpoint endpoint)
    {
    }

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(this);
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        //Write request message
        Console.WriteLine(request.ToString());
        return null;
    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        // Write out http headers
        foreach (var property in reply.Properties)
        {
            if (!(property.Value is HttpResponseMessageProperty)) continue;
            var httpProperties = (HttpResponseMessageProperty)property.Value;
            foreach (KeyValuePair<object, object> kvp in httpProperties.Headers)
            {
                Console.WriteLine(kvp.Key + ":" + kvp.Value);
            }
        }
        // Write result message
        Console.WriteLine(reply.ToString());
    }
}

Similarly you can write a logger on a service side with IDispatchMessageInspector and IServiceBehavior. 类似地,您可以使用IDispatchMessageInspector和IServiceBehavior在服务端编写记录器。

Love it, helped a lot. 喜欢它,帮了很多忙。 But in the future, you might want to tell us what using statements we may need. 但是在将来,您可能想告诉我们可能需要哪些using语句。

For everyone else, you will need to include: 对于其他人,您需要包括:

Using System.ServiceModel;
Using System.ServiceModel.Dispatcher;
Using System.ServiceModel.Channels;
Using System.ServiceModel.Description;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM