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

Tags: .NET, CodeSnippets, WCF, WinFX, WinFX January CTP

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    
            OperationContextScope(standardContextObject))
{
   ...
}


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)
      {
          contextScope.Dispose();
      }
    }     
 }


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?

8 Comments

  • 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.

    HTH
    -steve

  • 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

  • signsthathecaresaboutyou said

    freetypingwebpage freetypingwebpage,drvshanaukmaidstoneroadrainhamgillinghamkent drvshanaukmaidstoneroadrainhamgillinghamkent,joysetcapturedevc joysetcapturedevc,maidstonecouncil maidstonecouncil,riverbusalingtonemaidstone riverbusalingtonemaidstone,kathleenkarrshusband kathleenkarrshusband,adultonsat adultonsat,1940smoviestarpictures 1940smoviestarpictures,sbctelephonedisconnect sbctelephonedisconnect,tattospiercings tattospiercings,

  • searsgt14gardentractor said

    middlewichpaintball middlewichpaintball,katebroffenbrenerunionavoidanceintocontractnegotiation katebroffenbrenerunionavoidanceintocontractnegotiation,howtounblocktearducts howtounblocktearducts,hotelsforcrossdressersintheus hotelsforcrossdressersintheus,alwaysamp alwaysamp,katehudsonandshortshorts katehudsonandshortshorts,handpainteddressers handpainteddressers,sunsetautosaleschambersburgpa sunsetautosaleschambersburgpa,hess3029thrduticail hess3029thrduticail,stdsandunbornchildren stdsandunbornchildren,

  • bmwz3parkingbrakehandle said

    whatisthecurrentvalueofmy1962fordfairlane whatisthecurrentvalueofmy1962fordfairlane,durationoffrenchpresidentialmandate durationoffrenchpresidentialmandate,apartmentlistingforlondonontariocanada apartmentlistingforlondonontariocanada,blacklicoricestrings blacklicoricestrings,wwwcarolusthermeaachen wwwcarolusthermeaachen,myspaceglitterdolls myspaceglitterdolls,jackgracebebo jackgracebebo,robertoclementetimeline robertoclementetimeline,premadehollistermyspacelayouts premadehollistermyspacelayouts,supermangamesonlinefree supermangamesonlinefree,

  • 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

Comments have been disabled for this content.