2012-03-14 55 views
1

以下是這種情況:我試圖向中間路由器服務發送SOAP消息。該服務只關心我的SOAP消息標題,並使用WS-AddressingTo標題轉發我的消息。設置MessageHeaders.To字段被覆蓋

我需要基本POST像下面路由器服務的請求:

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 Behaviors沒有問題來設置路由服務需要其他自定義頁眉:

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場 - 它總是被重新設置爲相同的URI HTTP POST值。事實上,我使用.NET Reflector來調試這個字段的設置 - 當然,它正在被覆蓋(screenshot of the stack trace and breakpoint)。

有沒有其他方法可以讓我更改To SOAP標頭,但我不能正確理解?

回答

0

我想出了我自己的hint from here。以編程方式,我可以在custom behaviorClientRuntime上設置Via。這允許POST與由於使用WSHttpBinding而自動設置的實際端點地址不同。

public void ApplyClientBehavior 
    (ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
{ 
    CustomMessageInspector inspector = new CustomMessageInspector(); 
    clientRuntime.MessageInspectors.Add(inspector); 
    clientRuntime.Via = new Uri("http://gatewayRouter/routingService"); 
}