Stef Heyenrath
Stef Heyenrath

Reputation: 9820

How to programmatically add soap header when calling a WCF Service from .NET?

The service configuration in the app.config contains this setting:

<client>
  <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_GenerateAndConvert" contract="GenerateAndConvert.MyPortType" name="MyGenerateAndConvert" >
    <headers>
      <AuthHeader>
        <username>abc</username>
        <password>xyz</password>
      </AuthHeader>
    </headers>
  </endpoint>
</client>

which is serialized as:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:MyServer">
   <soapenv:Header>
       <AuthHeader>
            <username>abc</username>
            <password>xyz</password>
          </AuthHeader>
   </soapenv:Header>
   <soapenv:Body>
      <urn:Convert>
      </urn:Convert>
   </soapenv:Body>
</soapenv:Envelope>

My question is how to programmatically add the SOAP Header (instead defining the username and password in the config file) when using a Service Reference in a .NET application.

I tried a solution like https://stackoverflow.com/a/53208601/255966, but I got exceptions that scope was disposed on another thread, probably because I call the WCF service Async.

Upvotes: 1

Views: 1566

Answers (2)

Stef Heyenrath
Stef Heyenrath

Reputation: 9820

I got it working thanks to the comments/answers on this question and these additional resources:

My solution is as follows:

AuthHeader

Create AuthHeader class which extends MessageHeader to create the AuthHeader with username and password:

public class AuthHeader : MessageHeader
{
    private readonly string _username;
    private readonly string _password;

    public AuthHeader(string username, string password)
    {
        _username = username ?? throw new ArgumentNullException(nameof(username));
        _password = password ?? throw new ArgumentNullException(nameof(password));
    }

    protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
    {
        writer.WriteStartElement("username");
        writer.WriteString(_username);
        writer.WriteEndElement();

        writer.WriteStartElement("password");
        writer.WriteString(_password);
        writer.WriteEndElement();
    }

    public override string Name => "AuthHeader";

    public override string Namespace => string.Empty;
}

HttpHeaderMessageInspector

Create a HttpHeaderMessageInspector which can be used to add a MessageHeader to the request message.

public class HttpHeaderMessageInspector : IClientMessageInspector
{
    private readonly MessageHeader[] _headers;

    public HttpHeaderMessageInspector(params MessageHeader[] headers)
    {
        _headers = headers;
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        foreach (var header in _headers)
        {
            request.Headers.Add(header);
        }

        return null;
    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
    }
}

AddHttpHeaderMessageEndpointBehavior

Create a specific EndpointBehavior which uses the HttpHeaderMessageInspector.

public class AddHttpHeaderMessageEndpointBehavior : IEndpointBehavior
{
    private readonly IClientMessageInspector _httpHeaderMessageInspector;

    public AddHttpHeaderMessageEndpointBehavior(params MessageHeader[] headers)
    {
        _httpHeaderMessageInspector = new HttpHeaderMessageInspector(headers);
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.ClientMessageInspectors.Add(_httpHeaderMessageInspector);
    }
}

Client

Create a Client which adds a custom EndpointBehavior to insert the MessageHeader in the Soap message.

private MyTestPortTypeClient CreateAuthenticatedClient()
{
    var client = new MyTestPortTypeClient(_settings.EndpointConfigurationName, _settings.EndpointAddress);
    client.Endpoint.EndpointBehaviors.Clear();

    var authHeader = new AuthHeader(_settings.UserName, _settings.Password);
    client.Endpoint.EndpointBehaviors.Add(new AddHttpHeaderMessageEndpointBehavior(authHeader));

    return client;
}

Upvotes: 2

Ding Peng
Ding Peng

Reputation: 3954

You can add soap header in the implementation class by implementing IClientMessageInspector interface.

     public class ClientMessageLogger : IClientMessageInspector
{
    public void AfterReceiveReply(ref Message reply, object correlationState)
    {

    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        MessageHeader header = MessageHeader.CreateHeader("MySoapHeader", "http://my-namespace.com", "asdas");
        request.Headers.Add(header);
        return null;
    }
}

Add clientmessagelogger to clientruntime:

    [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = false)]
public class CustContractBehaviorAttribute : Attribute, IContractBehavior, IContractBehaviorAttribute
{
    public Type TargetContract => throw new NotImplementedException();

    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
        return;
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger());
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
        return;
    }
}

Add Attribute to Interface:

 [CustContractBehavior]
    public interface IService {
    }

This is the soap message received by the server:

enter image description here

To learn more about IClientMessageInspector, please refer to the following link.

Upvotes: 0

Related Questions