2016-08-12 59 views
9

我打了一個使用WSDL的WCF服務我無權訪問並且無法修改。對於因爲我們發送的遠程服務是死於請求之一:使用IClientMessageInspector從WCF肥皂請求中刪除操作節點mustUnderstand

<Action s:mustUnderstand="1"....> 

已經廣泛搜索,我不能找到一個簡單的解決我的問題。因此, 在一個典型的消息:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" /> 
    </s:Header> 
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <retrieveBooking xmlns="http://services.rccl.com/Interfaces/RetrieveBooking"> 
     <OTA_ReadRQ TransactionActionCode="RetrievePrice" SequenceNmbr="1" Version="1" xmlns="http://www.opentravel.org/OTA/2003/05/alpha"> 

我想我可以刪除該節點作爲報文檢查的一部分:

internal class MyMessageInspector : IClientMessageInspector 
{ 
    public object BeforeSendRequest(ref Message aRequest, IClientChannel aChannel) 
    { 
     //Get rid of mustUnderstand Action node 
     foreach (MessageHeaderInfo headerInfo in aRequest.Headers.UnderstoodHeaders) 
     { 
      aRequest.Headers.UnderstoodHeaders.Remove(headerInfo); 
     } 

     return null; 
    } 
} 

然而即使aRequest.Headers.UnderstoodHeaders爲空後,我刪除所有元素,我仍然看到Action節點在XML中發出。

  1. 我該做些什麼來完成這項工作?
  2. 如何獲取 消息內容,以便我可以檢查第一個節點的名稱 正文標籤retrieveBooking在這種情況下? (我只需要做 這是針對一個特定的消息,不是全部)

回答

1

而且答案最終很簡單。

public object BeforeSendRequest(ref Message aRequest, IClientChannel aChannel) 
{ 
    //For the CabinDetail message the API provider has requested that we REMOVE the XML action node from the header as it causes their end to fail 
    //<s:Header> 
    //<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" /> 
    //</s:Header> 
    if (aRequest.ToString().Contains("CabinDetail")) 
    { 
     int headerIndexOfAction = aRequest.Headers.FindHeader("Action", "http://schemas.microsoft.com/ws/2005/05/addressing/none"); 
     aRequest.Headers.RemoveAt(headerIndexOfAction); 
    } 

    return null; 
}