问题
Here's the scenario: I'm trying to send a SOAP message to an intermediary router service. That service only cares about my SOAP message headers, and uses the WS-Addressing To
header to forward along my message.
I need to basically POST a request like the following to the router service:
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 --->
I'm currently able to set other custom headers that the routing service requires by using Custom Behaviors without a problem:
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;
}
The above code works fine to add my CustomHeader to the message, but fails to modify the outgoing WS-Addressing To
field - it always gets set back to the same URI as the HTTP POST value. In fact, I used .NET Reflector to debug when this field gets set- and sure enough, it is getting overwritten (screenshot of the stack trace and breakpoint).
Is there some other way for me to change the To
SOAP header that I'm not understanding correctly?
回答1:
I figured it out on my own with a hint from here. Programatically, I can set the Via
on the ClientRuntime
inside the custom behavior. This allows the POST to differ from the actual endpoint address that gets set automatically due to my usage of WSHttpBinding.
public void ApplyClientBehavior
(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
CustomMessageInspector inspector = new CustomMessageInspector();
clientRuntime.MessageInspectors.Add(inspector);
clientRuntime.Via = new Uri("http://gatewayRouter/routingService");
}
来源:https://stackoverflow.com/questions/9711101/setting-messageheaders-to-field-gets-overwritten