如何从WCF中的客户端消息检查器中获取标头值

问题描述:

我正在为我正在处理的某些服务创建一个Web测试客户端,作为对此要求的一部分,我希望能够显示完整的请求和响应SOAP消息(以及HTTP标头用户).

I'm creating a web test client for some services I'm working on, and as part of the requirements for that, I would like to be able to display the full request and response SOAP messages (and HTTP headers to the user).

我实现了一个MessageInspector类,该类实现了 IClientMessageInspector ,最值得注意的是 BeforeSendRequest AfterReceiveReply 方法来访问请求&响应消息.

I implemented a MessageInspector class implementing IClientMessageInspector, most notably the BeforeSendRequest and AfterReceiveReply methods to get access to the request & response messages respectively.

捕获响应(AfterReceiveReply)效果很好,但是捕获请求仅部分起作用.我可以访问大多数消息,但是SOAP标头和HTTP标头都为空.在Fiddler中查看请求,我可以看到WCF在SOAP消息中发送了一个Security标头,并在HTTP标头中发送了一堆.

Capturing the response (AfterReceiveReply) works great, but capturing the request only partially works. I can access most of the message, however the SOAP header and HTTP headers are both empty. Viewing the request in Fiddler, I can see that WCF is sending a Security header in the SOAP message and "a bunch" of HTTP headers.

我的 BeforeSendRequest 方法非常简单...要点是...

My BeforeSendRequest method is very simple...the gist of it is...

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{  
    this.RequestMessage = request.ToString(); // Security header is missing from message

    // Try to get HTTP headers
    object req; // req is always null
    if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out req))
    {
        this.RequestHeaders = ((HttpRequestMessageProperty)req).Headers;
    }

    return null;
}

我不确定为什么缺少HTTP和Security标头.有更好的方法吗?

I'm not sure why the HTTP and Security headers are missing. Is there a better way to do this?

  • 韦恩

检查器会在消息存在格式化程序之后,到达任何可能会(可能)发生的协议通道(例如安全性)之前立即查看消息在传递消息之前先进行更改(请参阅有关WCF通道的帖子中的图,位于 http://blogs.msdn.com/b/carlosfigueira/archive/2011/07/12/wcf-extensibility-channels.aspx ).因此,在检查员级别,您将无法找到该消息添加的任何其他SOAP标头.HTTP头由传输添加的,在消息通过消息检查器之后也将到达该传输.

The inspectors look at the messages right after the message exists the formatter, and before it reaches any of the protocol channels (such as security) which will (potentially) change the message before passing it over (see the diagram in the post about WCF channels at http://blogs.msdn.com/b/carlosfigueira/archive/2011/07/12/wcf-extensibility-channels.aspx). So at the inspector level you won't be able to find any additional SOAP headers added by the message. The HTTP headers are added by the transport which is also reached after the message passes through the message inspector.

如果要查看消息中的所有SOAP标头,则可以创建一个新的协议"通道(该示例位于

If you want to see all the SOAP headers in the message, you can either create a new "protocol" channel (the sample at http://msdn.microsoft.com/en-us/library/ms751495.aspx does exactly that) or a new message encoder (it can wrap the existing encoder, and inspect the outgoing messages right before they're encoded).

要想看到HTTP标头,就更难了,因为传输是消息在WCF中传递的最后一部分.我认为您可以编写一个自定义的传输渠道来做到这一点,但这肯定是很多代码.

To be able to see the HTTP headers it's harder, since the transport is the last part through which the message passes in WCF. I think you could write a custom transport channel to do that, but that would definitely be a lot of code.