Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations

How do I add a custom header to every WCF message?

One thing I still haven't managed to do is create my proxy in such a way that a custom header is added to every call that is made through that proxy.
Currently, this is done by instantiating a new OperationContext around each call through the proxy. This can be optimized in several ways:
1) Create a shared OperationContext object and always instatiate the OperationContextScope with it:
using (OperationContextScope scope = new    

I don't know if it's a good idea, though. Should OperationContext objects be persisted between calls, or would it have unexpected side effects?

2) Create a wrapper around the OperationContextScope class that fills the OperationContext with the proper headers:

public class MyOperationContextScope : IDisposable
   OperationContextScope contextScope;

   public MyOperationContextScope ()
      contextScope = new OperationContextScope();
      MessageHeader standardHeader = new   
      MessageHeader("StringHeaderValue").GetUntypedHeader("StringHeader", "ns");
      OperationContext.Current.OutgoingMessageHeaders.Add(standardHeader );

    public void Dispose()
      if (contextScope != null)

This is a bit cleaner, but still requires me to wrap every proxy call with a using() statement.

Is there a way to interecept all calls and have my headers added automatically?


Steve Maine said:

One way to do this at the dispatcher layer is by implementing a message inspector. Message inspectors get to look at each and every message before it hits the dispatch infrastructure. On the client side we have IClientMessageInspector and IDispatchMessageInspector on the server.

You can hook them in with by implementing IEndpointBehavior, and adding your instance to the EndpointDispatcher.DispatchRuntime.MessageInspectors collection during ApplyDispatchBehavior(). The process is similar for the client side.

If your headers are there to do protocol-ish stuff, you can think about adding them in via a layered channel instead.

There should be samples for all of this in the SDK by the time we ship.

# April 26, 2006 5:02 AM

Avner Kashtan said:

Huh. I saw your reply just after I found out how to do it myself. It's very cool. I'd love to hear if I did anything wrong there. :)

I'm still a bit vague about the names of different features. When I see "ClientBehavior" or "ProxyBehavior", I know it'll happen client-side.
The IEndpointBehavior interface, though, gives me a ServiceEndpoint and an EndpointListener - both seem at first glance to be server-side, right?
# April 26, 2006 5:21 AM

vishal said:

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
# May 5, 2006 6:54 AM

signsthathecaresaboutyou said:

<a href="httpwwwrrsanfhtcnpage55html">freetypingwebpage</a> freetypingwebpage,<a href="httpwwwrrsanfhtcnpage64html">drvshanaukmaidstoneroadrainhamgillinghamkent</a> drvshanaukmaidstoneroadrainhamgillinghamkent,<a href="httpwwwrrsanfhtcnpage73html">joysetcapturedevc</a> joysetcapturedevc,<a href="httpwwwrrsanfhtcnpage64html">maidstonecouncil</a> maidstonecouncil,<a href="httpwwwrrsanfhtcnpage64html">riverbusalingtonemaidstone</a> riverbusalingtonemaidstone,<a href="httpwwwrrsanfhtcnpage50html">kathleenkarrshusband</a> kathleenkarrshusband,<a href="httpwwwrrsanfhtcnpage77html">adultonsat</a> adultonsat,<a href="httpwwwrrsanfhtcnpage58html">1940smoviestarpictures</a> 1940smoviestarpictures,<a href="httpwwwrrsanfhtcnpage76html">sbctelephonedisconnect</a> sbctelephonedisconnect,<a href="httpwwwrrsanfhtcnpage60html">tattospiercings</a> tattospiercings,

# August 1, 2007 1:18 PM

searsgt14gardentractor said:

<a href="httpwwwnkbsrozhcnpage81html">middlewichpaintball</a> middlewichpaintball,<a href="httpwwwnkbsrozhcnpage64html">katebroffenbrenerunionavoidanceintocontractnegotiation</a> katebroffenbrenerunionavoidanceintocontractnegotiation,<a href="httpwwwnkbsrozhcnpage63html">howtounblocktearducts</a> howtounblocktearducts,<a href="httpwwwnkbsrozhcnpage84html">hotelsforcrossdressersintheus</a> hotelsforcrossdressersintheus,<a href="httpwwwnkbsrozhcnpage67html">alwaysamp</a> alwaysamp,<a href="httpwwwnkbsrozhcnpage64html">katehudsonandshortshorts</a> katehudsonandshortshorts,<a href="httpwwwnkbsrozhcnpage84html">handpainteddressers</a> handpainteddressers,<a href="httpwwwnkbsrozhcnpage60html">sunsetautosaleschambersburgpa</a> sunsetautosaleschambersburgpa,<a href="httpwwwnkbsrozhcnpage69html">hess3029thrduticail</a> hess3029thrduticail,<a href="httpwwwnkbsrozhcnpage61html">stdsandunbornchildren</a> stdsandunbornchildren,

# August 2, 2007 2:14 AM

bmwz3parkingbrakehandle said:

<a href="httpwwwdcxmsdykcnpage92html">whatisthecurrentvalueofmy1962fordfairlane</a> whatisthecurrentvalueofmy1962fordfairlane,<a href="httpwwwhknonnjzcnpage1html">durationoffrenchpresidentialmandate</a> durationoffrenchpresidentialmandate,<a href="httpwwwdcxmsdykcnpage95html">apartmentlistingforlondonontariocanada</a> apartmentlistingforlondonontariocanada,<a href="httpwwwhknonnjzcnpage8html">blacklicoricestrings</a> blacklicoricestrings,<a href="httpwwwdcxmsdykcnpage86html">wwwcarolusthermeaachen</a> wwwcarolusthermeaachen,<a href="httpwwwhknonnjzcnpage4html">myspaceglitterdolls</a> myspaceglitterdolls,<a href="httpwwwhknonnjzcnpage9html">jackgracebebo</a> jackgracebebo,<a href="httpwwwhknonnjzcnpage3html">robertoclementetimeline</a> robertoclementetimeline,<a href="httpwwwdcxmsdykcnpage98html">premadehollistermyspacelayouts</a> premadehollistermyspacelayouts,<a href="httpwwwdcxmsdykcnpage99html">supermangamesonlinefree</a> supermangamesonlinefree,

# August 2, 2007 7:14 AM

Betty said:

Gear up for grub with a tripleheader of pigskin, including a meeting of brothers in Dallas. Everybody knows it's been a rough year for her, but find out who else had issues

# November 22, 2007 7:20 AM

laptop notebook said:

2CVljo I really liked your post.Really looking forward to read more. Great.

# April 15, 2012 6:12 AM

crork said:

6d5DgJ I really enjoy the post.Thanks Again. Great.

# October 25, 2012 4:55 PM