C# 设置MessageHeaders.To字段将被覆盖

C# 设置MessageHeaders.To字段将被覆盖,c#,wcf,soap,wcf-client,C#,Wcf,Soap,Wcf Client,下面是一个场景:我试图向中间路由器服务发送SOAP消息。该服务只关心我的SOAP消息头,并使用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: K

下面是一个场景:我试图向中间路由器服务发送SOAP消息。该服务只关心我的SOAP消息头,并使用
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 --->
上面的代码可以很好地将my CustomHeader添加到消息中,但无法将传出的WS-Addressing
修改为
字段-它总是被设置回与HTTP POST值相同的URI。事实上,当这个字段被设置时,我使用.NET Reflector进行调试,果然,它被覆盖了()


是否有其他方法可以将
更改为我理解不正确的
SOAP头?

我自己用一个。通过编程,我可以通过
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");
}
public void ApplyClientBehavior
    (ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
    CustomMessageInspector inspector = new CustomMessageInspector();
    clientRuntime.MessageInspectors.Add(inspector);
    clientRuntime.Via = new Uri("http://gatewayRouter/routingService");
}