Search code examples
c#wcfsoapws-addressing

SOAP WCF WS-Addressing 'Multiple headers with name 'Action' and namespace 'http://schemas.microsoft.com/ws/2005/05/addressing/none' found.'


I'm working on a Client that communicate with a legacy system utilizing SOAP WCF WS-Addressing messages.

Additionally, its required to customize the SOAP-Envelope header with To and Action headers that contains custom information.

I was able to set the To and Action SOAP-Envelope header with the information through utilizing the OperationContextScope as shown in the code below:


public async Task<getAttorneyResponseStructure> GetAttorneyAsync(GetAttorneyRequestStructure getAttorneyRequestStructure)
{
  try
  {
    using (new OperationContextScope(Client.InnerChannel))
    {
      getAttorneyRequestStructure.AttorneyHeader = Header;

      OperationContext.Current.OutgoingMessageHeaders.To = new Uri("http://rydwvgsn01.spga.gov.sa/GSBExpress/Legal/MOJAttorneyInquiry/2.0/AttorneyInquiryService.svc");

      OperationContext.Current.OutgoingMessageHeaders.Action = "http://tempuri.org/IAttorneyInquiryService/GetAttorney";

      return await Client.GetAttorneyAsync(getAttorneyRequestStructure);
    }
  }
  catch (Exception e)
  {
   throw;
  }
}

When I run the code and try to send the message I end up with the an exception Multiple headers with name 'Action' and namespace 'http://schemas.microsoft.com/ws/2005/05/addressing/none' found.

By looking at the exception stack as attached in the picture, it seems there is an object containing the same information of the header that I'm trying to add.

enter image description here

So, my question is there a work around changing the Namespace of the Action header or modify the existing Action that containing the set Namespace?


Solution

  • Solved! It turned out that the issue I was using the default BasicHttpBinding that was using different soap version different from the server. Additionally, Action attribute in the header wasn't needed since I've changed SOAP version using CustomBinding as follow in the connection constructor:

    CustomBinding binding = new CustomBinding();
    
    var mtomMessageEncodingBindingElement = new MtomMessageEncodingBindingElement
    {
      MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.WSAddressing10),
    };
    binding.Elements.Add(mtomMessageEncodingBindingElement);
    
    var httpTransportBindingElement = new HttpTransportBindingElement();
    binding.Elements.Add(httpTransportBindingElement);
                
    Client = new AttorneyInquiryServiceClient(binding, new EndpointAddress("http://rydwvgsn01.spga.gov.sa/GSBExpress/Legal/MOJAttorneyInquiry/2.0/AttorneyInquiryService.svc"));
    
    

    Some additional explanation for the code above:

    • MtomMessageEncodingBindingElement: Was used to be able to set the version of the SOAP version and to enable MOTM type of response.

    • HttpTransportBindingElement: Is needed in the Binding.

    • In the method calling, Action was removed.

    public async Task<getAttorneyResponseStructure> GetAttorneyAsync(GetAttorneyRequestStructure getAttorneyRequestStructure)
    {
      try
      {
        using (new OperationContextScope(Client.InnerChannel))
        {
          getAttorneyRequestStructure.AttorneyHeader = Header;
    
          OperationContext.Current.OutgoingMessageHeaders.To = new Uri("http://rydwvgsn01.spga.gov.sa/GSBExpress/Legal/MOJAttorneyInquiry/2.0/AttorneyInquiryService.svc");
    
          return await Client.GetAttorneyAsync(getAttorneyRequestStructure);
        }
      }
      catch (Exception e)
      {
       throw;
      }
    }