3

我有一个 WCF 服务,它带有一个IDispatchMessageInspector修改BeforeSendReply消息的 WS-Addressing 标头的方法。这适用于所有标题,除了 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()(result = Messagetype) 给了我:

<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标题从我的回复中被剥离?

4

1 回答 1

2

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

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

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

<customBinding>
  <binding name="customBinding_manualAddressingEnabled">
    <textMessageEncoding />
    <httpTransport manualAddressing="true"/>
  </binding>
</customBinding>
于 2017-04-16T20:50:24.263 回答