WCF Custom Message Headers

23 באפריל 2006


WCF Custom Message Headers

WCF Custom Message Headers Often do we want to pass some data to some or maybe all our service operations. This data is usually context data such as user tokens, or environmental preferences of the user or machine. In these kind of situations, we would rather not add additional context parameters to the contracts or our services, because we don’t want to involve implementation data / context data with the business parameters of our services.

A nice and easy way to pass that data is to use MessageHeaders. In order to do this we follow these steps:

  • Add the context data to the outgoing message headers.

  • Call the operation (Nothing special here).

  • Extract the data from the incoming message headers.

Using an OperationContextScope

In order to add the message header, you should have a OperationContext for your call. An OperationContext will automaticly be created for the call, and will be available in the service side. If one wishes to add the message headers to the OperationContext before the actual operation call, he should use an OperationContextScope, like the following piece of code shown as Code Block 1.

Notice that the value that is being passed to the constructor of OperationContextScope must implement IContextChannel interface.


Adding the context data to the outgoing message headers (client-side)

In the example shown in Code Block 1, there is a Guid token that had to be passed in the message headers. In order to pass it, I created a typed MessageHeader, and than produced an untyped MessageHeader with a name and namespace.


Code Block 1:


using (ChannelFactory<IMyServiceChannel> factory =

   new ChannelFactory<IMyServiceChannel>(new NetTcpBinding()))


   using (IMyServiceChannel proxy = factory.CreateChannel())


      using ( OperationContextScope scope =

               new OperationContextScope(proxy) )


         Guid myToken = Guid.NewGuid();

         MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken);

         MessageHeader untyped = mhg.GetUntypedHeader("token", "ns");








Extract the data from the incoming message headers (service-side)

The following code (Code Block 2) shows how simple it is to extract the typed message header from the incoming headers collection of the OperationContext.


Code Block 2:


Guid myToken =


         GetHeader<Guid>("token", "ns");


You can download the sample project that shows the usage of MessageHeader in the OperationContext from here.

The Samples are all based on the Base Service Solutions that I posted earlier in the Blog.



Add comment
facebook linkedin twitter email


  1. Rafael Montero24 במאי 2007 ב 23:02

    Excellent post, I had to spent lots of times to get to this blog. Maybe you should send it to the MSDN documentation people 😉

  2. escortservLatvian28 בנובמבר 2007 ב 22:48

    Riga,Latvia best escort girls, massage salons, escort boys and sauna. All information about adult's entertainment in Riga, Latvia.

  3. escortservLatvians14 בינואר 2008 ב 5:45

    Riga,Latvia best escort girls, massage salons, escort boys and sauna. All information about adult's entertainment in Riga, Latvia.
    http://www.ladies.lv friend http://www.escort-service.lv .

  4. Kudoro Seun28 בינואר 2008 ב 13:23

    Hi Guy,
    I am trying to add custom headers to a an httpRequest but i have not bee successful. Could you please help with a working code. You can get back to tannamario@yahoo.com

  5. ...10 במרץ 2008 ב 5:26

    [URL=http://www.rubesch.com/anti-spam] anti spam [/URL] anti spam

  6. ...14 במרץ 2008 ב 16:23

    [URL=http://www.cedricleroy.com/hostels] hostels [/URL] hostels

  7. ...14 במרץ 2008 ב 22:11

    [URL=http://www.cedricleroy.com/creare-logo] creare logo [/URL] creare logo

  8. ...15 במרץ 2008 ב 15:31

    [URL=http://www.huraiyth.com/servizio-adsl] servizio adsl [/URL] servizio adsl

  9. ...15 במרץ 2008 ב 21:13

    [URL=http://www.huraiyth.com/tenerife-voli] tenerife voli [/URL] tenerife voli

  10. ...16 במרץ 2008 ב 3:01

    [URL=http://www.betamate.com/management-training] management training [/URL] management training

  11. ...16 במרץ 2008 ב 16:14

    [URL=http://www.betamate.com/csa-salerno] csa salerno [/URL] csa salerno

  12. ...16 במרץ 2008 ב 22:21

    [URL=http://www.ncfliving.net/sfondo-inter] sfondo inter [/URL] sfondo inter

  13. ...17 במרץ 2008 ב 4:35

    [URL=http://www.ncfliving.net/192-168-2-1] 192 168 2 1 [/URL] 192 168 2 1

  14. ...17 במרץ 2008 ב 10:31

    [URL=http://www.key4fun.com/eeyore] eeyore [/URL] eeyore

  15. ...18 במרץ 2008 ב 0:27

    [URL=http://www.fxtend.net/toscana-it] toscana it [/URL] toscana it

  16. ...18 במרץ 2008 ב 6:35

    [URL=http://www.fxtend.net/bruce-willis] bruce willis [/URL] bruce willis

  17. ...18 במרץ 2008 ב 13:14

    [URL=http://www.michaelsteven.net/milf-riders] milf riders [/URL] milf riders

  18. ...18 במרץ 2008 ב 13:14

    [URL=http://www.michaelsteven.net/milf-riders] milf riders [/URL] milf riders

  19. ...18 במרץ 2008 ב 23:29

    [URL=http://www.michaelsteven.net/atomic-kitten] atomic kitten [/URL] atomic kitten

  20. ...19 במרץ 2008 ב 6:08

    [URL=http://www.michaelsteven.net/blocchi-autocad] blocchi autocad [/URL] blocchi autocad

  21. ...19 במרץ 2008 ב 13:02

    [URL=http://www.youngamericansfund.com/libri-italiano] libri italiano [/URL] libri italiano

  22. ...20 במרץ 2008 ב 8:10

    [URL=http://www.myshots.info/ilaria-d-amico.php] Ilaria d amico [/URL] Ilaria d amico

  23. anil prasad31 במאי 2008 ב 14:24

    Thanks for your post. Do you know of any security implications of sending custom headers? what i mean is if i enable message level security, will WCF on client secure this custom header before its given to transport?


  24. boxandwh2 ביוני 2008 ב 1:18


  25. accelpFes10 באפריל 2009 ב 11:21

    The father makes lustrous that this is … With dissolution, Diamond hopes to jog our collective retention to restrain us from …
    [url=http://alexsisk.jatsu.pl]Good Dating Resource[/url]

Comments are closed.