Mark Willis
Mark Willis

Reputation: 848

Prevent "To" SOAP header being added

I'm have a problem with a SOAP header created in my C# client. The server is sending back the error

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <s:Header xmlns:s="http://www.w3.org/2003/05/soap-envelope" />
  <soap:Body>
    <soap:Fault>
      <soap:Code>
        <soap:Value>soap:MustUnderstand</soap:Value>
      </soap:Code>
      <soap:Reason>
        <soap:Text xml:lang="en">MustUnderstand headers: [{http://www.w3.org/2005/08/addressing}To] are not understood.</soap:Text>
      </soap:Reason>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

I have been under the impression that I have been removing all SOAP headers with the following code.

internal class CustomMessageInspector : IEndpointBehavior, IClientMessageInspector
{
    public object BeforeSendRequest( ref Message request, IClientChannel channel )
    {
        request.Headers.Clear();
        return null;
    }
    ...
 }

However, after activating System.ServiceModel.MessageLogging in the app.config, (WCF - Inspect the messages being sent/received?), I see that the server is correct - lo and behold there is a "To" header with "mustUnderstand" set to 1 :

<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">https://ws-single-transactions-int-bp.nmvs.eu:8443/WS_SINGLE_TRANSACTIONS_V1/SinglePackServiceV30</a:To>
</s:Header>

Any thoughts how I can prevent this header from being added?

Many thanks.

Upvotes: 4

Views: 1127

Answers (1)

Mark Willis
Mark Willis

Reputation: 848

In case it helps anybody else, I have found a solution. In fact Nicolas Giannone provided all the necessary code here WSHttpBinding in .NetStandard or .NET core . What we can do is to replace the WSHttpBinding with a custom binding based on the WSHttpBinding, and then replace the TextMessageEncodingBindingElement with one with no addressing. Here's the code :

    string endPoint = myConfig.SinglePackServicesEndPoint;

    //Defines a secure binding with certificate authentication
    WSHttpBinding binding = new WSHttpBinding();
    binding.Security.Mode = SecurityMode.Transport;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

    // create a new binding based on the existing binding
    var customTransportSecurityBinding = new CustomBinding( binding );
    // locate the TextMessageEncodingBindingElement - that's the party guilty of the inclusion of the "To"
    var ele = customTransportSecurityBinding.Elements.FirstOrDefault( x=>x is TextMessageEncodingBindingElement );
    if( ele != null )
    {
        // and replace it with a version with no addressing
        // replace {Soap12 (http://www.w3.org/2003/05/soap-envelope) Addressing10 (http://www.w3.org/2005/08/addressing)}
        //    with {Soap12 (http://www.w3.org/2003/05/soap-envelope) AddressingNone (http://schemas.microsoft.com/ws/2005/05/addressing/none)}
        int index = customTransportSecurityBinding.Elements.IndexOf( ele );
        var textBindingElement = new TextMessageEncodingBindingElement
        {
            MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
        };
        customTransportSecurityBinding.Elements[index] = textBindingElement;
    }

Upvotes: 5

Related Questions