Search code examples
c#web-serviceswcfsoap

Remove Action Node mustUnderstand from WCF soap request using IClientMessageInspector


I am hitting a WCF service using a WSDL I don't have access to and cannot modify. For one of the requests the remote service is dying because we are sending the:

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

Having searched extensively I cannot find a simple solution to my problem. So, in a typical message:

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

I figured I could remove this node as part of message inspector:

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

however even though the aRequest.Headers.UnderstoodHeaders is empty after I remove all the elements, I am still seeing the Action node being emitted in the XML.

  1. What do I have to do to make this work?
  2. How do I get at the message contents so that I can inspect the name of the first node of the body tag retrieveBooking in this case? (I only need to do this for a specific message, not all of them)

Solution

  • And the answer ends up being very simple in the end.

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