设置MessageHeaders.To字段会被覆盖

时间:2012-03-14 22:13:34

标签: c# wcf soap wcf-client

这是场景:我正在尝试将SOAP消息发送到中间路由器服务。该服务仅关注我的SOAP消息头,并使用WS-Addressing To标头转发我的消息。

我需要基本上将以下请求发送到路由器服务:

POST http://gatewayRouter/routingService HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Host: gatewayRouter
Content-Length: 8786
Expect: 100-continue
Connection: Keep-Alive

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
    xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header> <!-- ... --> 
<a:To s:mustUnderstand="1">http://actualDestination</a:To>
</s:Header> <!-- ... body, /envelope, etc --->

我目前能够使用Custom Behaviors设置路由服务所需的其他自定义标头,而不会出现问题:

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
    MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
    request = buffer.CreateMessage();
    request.Headers.To = new Uri("http://actualDestination");
    request.Headers.Add(new CustomHeader());
    return null;
}

上面的代码可以很好地将我的CustomHeader添加到消息中,但无法修改传出的WS-Addressing To字段 - 它总是被设置回与HTTP POST值相同的URI。事实上,我使用.NET Reflector来调试这个字段何时被设置 - 当然,它被覆盖了(screenshot of the stack trace and breakpoint)。

我还有其他方法可以更改我不能正确理解的To SOAP标头吗?

1 个答案:

答案 0 :(得分:0)

我用hint from here自己想出来了。以编程方式,我可以在custom behavior内的Via上设置ClientRuntime。这允许POST与由于我使用WSHttpBinding而自动设置的实际端点地址不同。

public void ApplyClientBehavior
    (ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
    CustomMessageInspector inspector = new CustomMessageInspector();
    clientRuntime.MessageInspectors.Add(inspector);
    clientRuntime.Via = new Uri("http://gatewayRouter/routingService");
}