Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations
Adding custom headers to every WCF call - a solution
At last, a solution presented itself. While I must admit that at first I was very skeptical of the extensibilty model for WCF which seemed far too involved and complicated, but after implementing a simple extension I must say it's simple and quite intuitive.

My goal was to have several custom headers added to each call made through a proxy, rather than manually adding them to each call's OperationContext. With a little help from Ralph Squillace's blog, I was able to get an extension up and running within minutes, and it Just Works.

The first item to build is the actual extension logic. In this case, I needed an IProxyMessageInspector object that inspects each outgoing message and adds the custom headers:

public class ContextPusher : IProxyMessageInspector
    {
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            // Do nothing.
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            MessageHeader myHeader = new MessageHeader("HeaderValue").GetUntypedHeader("MyHeader", "ns");
            request.Headers.Add(
myHeader);
            return null;
        }
    }
}

Now we want to attach that inspector to proxy. We do that wth Behavior objects. This can be done on an operation-by-operation basis or for all operations on a given channel. We can make the same class implement both behaviors, for flexibility:

public class CallContextAttribute : Attribute, IChannelBehavior, IOperationBehavior
    {
        #region IOperationBehavior Members

        public void ApplyBehavior(OperationDescription description, ProxyOperation proxy, BindingParameterCollection parameters)
        {      
            // Add my MessageInspector to each message sent by the proxy.
            proxy.Parent.MessageInspectors.Add(new ContextPusher());
  
        }

        public void ApplyBehavior(OperationDescription description, DispatchOperation dispatch, BindingParameterCollection parameters)
        {
            // No server-side behaviors for now.
        }

        #endregion
               
        #region IChannelBehavior Members

        public void ApplyBehavior(ChannelDescription description, ProxyBehavior behavior, BindingParameterCollection parameters)
        {
            // Add the MessageInspector to every message sent through the channel.
            behavior.MessageInspectors.Add(new ContextPusher());
        }

        #endregion
    }


And then simply put it on a Service or Operation.
I think in this case, putting the attribute on both will result in an error when trying to add duplicate headers. But this allows me flexibility in adding the headers only to certain calls.

[ServiceContract]
[CallContext]
public interface ILocalizationServices
{
   [OperationContract]
   [CallContext]
   string DoSomething(string param);
}


I think this is the first time I got really excited by the WCF framework and the ease of using and extending it. This is FUN.

Published Wednesday, April 26, 2006 11:20 AM by AvnerK

Comments

# re: Adding custom headers to every WCF call - a solution@ Friday, May 05, 2006 5:16 AM

Hi Am having same requiremnts i need to pass Headers to 3rd Party Web Service Using WCF. I just know tht the Web service Need some headers which i need to pass. I cant modify Web Service whatevr i have to do is through WCF cleint. Can u please explain the code above i mean ContextPusher & CallContextAttribute shld be the class on Client Application or on Service Side?

Vishal

# re: Adding custom headers to every WCF call - a solution@ Friday, May 05, 2006 6:57 AM

Hi Can u please send me SERVICE & WCF Client Code. I cant modify 3rd Party Web Service still i want to pass headers using WCF behaviour. Please PLease Help me...if u can mail me the code tht will be of great help am using FEB CTP

vishal

# WCF and Non-polymorphic bindings, <small>or How to write an incomprehensible post title</small>@ Monday, July 31, 2006 6:49 AM

I've been using WCF for the past few months and on the whole, I think the programming model works.

Miscellaneous Debris

# re: Adding custom headers to every WCF call - a solution@ Thursday, November 15, 2007 5:53 PM

This is pretty neat way doing than using the OperationContext to do the same.

Can you please send me the client and server code.

You can email at giri.kalluri@gmail.com

GKALLURI

# re: Adding custom headers to every WCF call - a solution@ Thursday, July 03, 2008 7:31 AM

Hi, can u share this code?

Saran

Leave a Comment

(required) 
(required) 
(optional)
(required)