问题
I have a WCF service with an IDispatchMessageInspector
and a BeforeSendReply
method which modifies the message's WS-Addressing headers. This works for all headers, except for wsa:To, which is being stripped from the reply...
public void BeforeSendReply(ref Message reply, object correlationState)
{
reply.Headers.To = new Uri("urn:something:something:something"); // Why won't this show up in the response?
reply.Headers.From = new EndpointAddress("urn:blabla:blabla");
reply.Headers.MessageId = MessageIDHelper.CreateNew();
reply.Headers.ReplyTo = new EndpointAddress(Definitions.WSA_REPLYTO_ANONYMOUS);
reply.Headers.Action = Definitions.WSA_ACTION_SOMETHING_SOMETHING;
}
This results in:
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://xxx.xx/xxx/Messages/1/Send</a:Action>
<a:RelatesTo>SOME_ID_WHATEVER</a:RelatesTo>
<a:From>
<a:Address>urn:xxx.xx:xxx:xxx</a:Address>
</a:From>
<a:MessageID>urn:uuid:083b5fb7-ff45-4944-b881-b4c590577408</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
</s:Header>
...
</s:Envelope>
Even though result.ToString()
(result = Message
type) gives me:
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://xxx.xx/xxx/Messages/1/Send</a:Action>
<a:RelatesTo>SOME_ID_WHATEVER</a:RelatesTo>
<a:To s:mustUnderstand="1">urn:xxx.xx:xxx:xxx<a:To>
<a:From>
<a:Address>urn:xxx.xx:xxx:xxx</a:Address>
</a:From>
<a:MessageID>urn:uuid:083b5fb7-ff45-4944-b881-b4c590577408</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
</s:Header>
...
</s:Envelope>
So... Why is the wsa:To
header stripped from my reply?
回答1:
The documentation of the TransportBindingElement.ManualAddressing property gives some information about the addressing behavior. I.e. if the value of ManuelAddressing is set to false, the sending channel applies the EndpointAddress configured as the To: addressee on the channel to outgoing messages. This implies that the channel has a say in the value of the To: header.
Now, the BeforeSendReply()
modifies the content of the message at the service level, before handing it over to the channel for transmission. Therefore, if the value of ManuelAddressing is false, the channel will set its own To: value in the message header.
Whenever the value of ManuelAddressing is set to true, the channel assumes the message has already been addressed and does not add any additional information. In order to set ManuelAddressing to True, a custombinding can be created in the web.config file:
<customBinding>
<binding name="customBinding_manualAddressingEnabled">
<textMessageEncoding />
<httpTransport manualAddressing="true"/>
</binding>
</customBinding>
来源:https://stackoverflow.com/questions/42701721/why-is-the-wsato-header-in-my-reply-message-removed-by-wcf