为什么wsa:WCF删除了我的回复邮件中的标题?

时间:2017-03-09 17:24:38

标签: c# wcf soap ws-addressing

我有一个带有IDispatchMessageInspectorBeforeSendReply方法的WCF服务,该方法修改了消息的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()(结果= 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标题会从我的回复中删除?

1 个答案:

答案 0 :(得分:1)

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

现在,BeforeSendReply()在将消息传递到通道进行传输之前,在服务级别修改消息的内容。因此,如果ManuelAddressing的值为false,则通道将在邮件头中设置自己的To:值。

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

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