Adding data to WCF message headers (client-side)

When working with WCF you may find a need to add data to the message headers, for example maybe you wish to add some form of authentication token or the likes.

The first thing we need to do is create a BehaviorExtensionElement and add the relevant configuration to the App.Config.

public class SecurityTokenEndpointBehaviourExtensionElement : BehaviorExtensionElement
{
   public override Type BehaviorType
   {
      get { return typeof(SecurityTokenEndpointBehaviour); }
   }
   protected override object CreateBehavior()
   {
      return new SecurityTokenEndpointBehaviour();
   }
}

Now we need the corresponding entry in the config file for the extension and also the behaviour that our endpoint will map to

<behaviors>
   <endpointBehaviors>
      <behavior name="serviceEndpointBehaviour">
         <securityTokenExtension />
      </behavior>
   </endpointBehaviors>
</behaviors>
<extensions>
   <behaviorExtensions>
      <add name="securityTokenExtension"
	type="PutridParrot.SecurityTokenEndpointBehaviourExtensionElement, PutridParrot" />
   </behaviorExtensions>
</extensions>

As you can see the configuration basically tells WCF to use our BehaviorExtensionElement which then creates the behaviour. This behaviour then adds inspectors to the client as in the sample below

public class SecurityTokenEndpointBehaviour : IEndpointBehavior
{
   public void AddBindingParameters(ServiceEndpoint endpoint, 
                   BindingParameterCollection bindingParameters)
   {
   }

   public void ApplyClientBehavior(ServiceEndpoint endpoint, 
                   ClientRuntime clientRuntime)
   {
      clientRuntime.MessageInspectors.Add(new SecurityTokenMessageInspector());
   }

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

   public void Validate(ServiceEndpoint endpoint)
   {
   }
}

So this code is very simple. In this case we’re simply adding a new message inspector and it’s this IClientMessageInspector implementation that adds the token to the WCF headers. As shown below

public class SecurityTokenMessageInspector : IClientMessageInspector
{
   public object BeforeSendRequest(ref Message request, IClientChannel channel)
   {
      request.Headers.Add(MessageHeader.CreateHeader("myToken", 
                          String.Empty, SecurityToken.Token));
      return null;
   }

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

The code above adds a new message header with the name “myToken” and a value of SecurityToken.Token (just assume this is a singleton which creates and stores a security token).

Before this will work on the client we need to configure the end point to use the behaviorConfiguration defined previously, so now in the endpoint add

behaviorConfiguration="serviceEndpointBehaviour"

So the client configuration will look something like

<client>
   <endpoint address="http://localhost:9095/MyService" 
            binding="basicHttpBinding" contract="MyService.IMyService"                    
            behaviorConfiguration="serviceEndpointBehaviour"/>
</client>