设置 MessageHeaders.To 字段被覆盖 [英] Setting MessageHeaders.To field gets overwritten

查看:28
本文介绍了设置 MessageHeaders.To 字段被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景如下:我试图将 SOAP 消息发送到中间路由器服务.该服务只关心我的 SOAP 消息标头,并使用 WS-AddressingTo 标题沿我的消息转发.

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

我目前可以使用 Custom 设置路由服务需要的其他自定义标头行为没有问题:

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

上面的代码可以很好地将我的 CustomHeader 添加到消息中,但无法修改传出的 WS-Addressing To 字段 - 它总是被设置回与 HTTP POST 值相同的 URI.事实上,当这个字段被设置时,我使用 .NET Reflector 进行调试 - 果然,它被覆盖了(堆栈跟踪和断点的屏幕截图).

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

是否有其他方法可以更改我无法正确理解的 To SOAP 标头?

Is there some other way for me to change the To SOAP header that I'm not understanding correctly?

推荐答案

我自己想出了一个此处提示.以编程方式,我可以在 自定义行为.这允许 POST 与由于我使用 WSHttpBinding 而自动设置的实际端点地址不同.

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

这篇关于设置 MessageHeaders.To 字段被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆