为什么 WCF 删除了我的回复消息中的 wsa:To header?

Why is the wsa:To header in my reply message removed by WCF?

我有一个带有 IDispatchMessageInspectorBeforeSendReply 方法的 WCF 服务,它修改消息的 WS-Addressing headers。这适用于所有 header,除了 wsa:To,它正在从回复中删除...

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;
}

这导致:

<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>

即使 result.ToString()(结果 = Message 类型)给我:

<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>

所以...为什么 wsa:To header 从我的回复中删除?

TransportBindingElement.ManualAddressing 属性 的文档提供了一些有关寻址行为的信息。 IE。如果 ManuelAddressing 的值设置为 false,则发送通道将通道上配置为 To: addressee 的 EndpointAddress 应用于传出消息。这意味着频道对 To 的值有发言权:header.

现在,BeforeSendReply()在服务级别修改消息的内容,然后将其交给频道进行传输。因此,如果 ManuelAddressing 的值为 false,通道将在消息中设置自己的 To: 值 header.

只要 ManuelAddressing 的值设置为 true,通道就会假定消息已被寻址并且不会添加任何附加信息。 为了将 ManuelAddressing 设置为 True,可以在 web.config 文件中创建自定义绑定:

<customBinding>
  <binding name="customBinding_manualAddressingEnabled">
    <textMessageEncoding />
    <httpTransport manualAddressing="true"/>
  </binding>
</customBinding>