DCSIMG
WCF Custom Message Headers - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

Links

Articles

Blogs I Read

WCF Custom Message Headers

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");

         OperationContext.Current.OutgoingMessageHeaders.Add(untyped);

 

         proxy.DoOperation(...);

      }

   }                   

}

 

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 =

OperationContext.Current.IncomingMessageHeaders.

         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.

 

Enjoy!

Comments

Guy Burstein's Blog said:

This is my #200 post, and it is a good time to look back at what I've posted over the last few months.

# March 9, 2007 8:46 PM

Rafael Montero said:

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

# May 24, 2007 11:02 PM

Guy Burstein's Blog said:

Over the past year I've written almost 300 posts, and recently I took the time to see which posts were

# July 29, 2007 9:34 PM

escortservLatvian said:

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

<a href=http://www.ladies.lv></a>

# November 28, 2007 10:48 PM

escortservLatvians said:

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 www.escort-service.lv .

# January 14, 2008 5:45 AM

Kudoro Seun said:

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

# January 28, 2008 1:23 PM

... said:

[URL=www.rubesch.com/anti-spam] anti spam [/URL]   <a href='www.rubesch.com/anti-spam'> anti spam </a>

# March 10, 2008 5:26 AM

... said:

[URL=www.cedricleroy.com/hostels] hostels [/URL]   <a href='www.cedricleroy.com/hostels'> hostels </a>

# March 14, 2008 4:23 PM

... said:

[URL=www.cedricleroy.com/creare-logo] creare logo [/URL]   <a href='www.cedricleroy.com/creare-logo'> creare logo </a>

# March 14, 2008 10:11 PM

... said:

[URL=www.huraiyth.com/servizio-adsl] servizio adsl [/URL]   <a href='www.huraiyth.com/servizio-adsl'> servizio adsl </a>

# March 15, 2008 3:31 PM

... said:

[URL=www.huraiyth.com/tenerife-voli] tenerife voli [/URL]   <a href='www.huraiyth.com/tenerife-voli'> tenerife voli </a>

# March 15, 2008 9:13 PM

... said:

[URL=www.betamate.com/management-training] management training [/URL]   <a href='www.betamate.com/management-training'> management training </a>

# March 16, 2008 3:01 AM

... said:

[URL=www.betamate.com/csa-salerno] csa salerno [/URL]   <a href='www.betamate.com/csa-salerno'> csa salerno </a>

# March 16, 2008 4:14 PM

... said:

[URL=www.ncfliving.net/sfondo-inter] sfondo inter [/URL]   <a href='www.ncfliving.net/sfondo-inter'> sfondo inter </a>

# March 16, 2008 10:21 PM

... said:

[URL=www.ncfliving.net/192-168-2-1] 192 168 2 1 [/URL]   <a href='www.ncfliving.net/192-168-2-1'> 192 168 2 1 </a>

# March 17, 2008 4:35 AM

... said:

[URL=http://www.key4fun.com/eeyore] eeyore [/URL]   <a href='http://www.key4fun.com/eeyore'> eeyore </a>

# March 17, 2008 10:31 AM

... said:

[URL=www.fxtend.net/toscana-it] toscana it [/URL]   <a href='www.fxtend.net/toscana-it'> toscana it </a>

# March 18, 2008 12:27 AM

... said:

[URL=www.fxtend.net/bruce-willis] bruce willis [/URL]   <a href='www.fxtend.net/bruce-willis'> bruce willis </a>

# March 18, 2008 6:35 AM

... said:

[URL=www.michaelsteven.net/milf-riders] milf riders [/URL]   <a href='www.michaelsteven.net/milf-riders'> milf riders </a>

# March 18, 2008 1:14 PM

... said:

[URL=www.michaelsteven.net/milf-riders] milf riders [/URL]   <a href='www.michaelsteven.net/milf-riders'> milf riders </a>

# March 18, 2008 1:14 PM

... said:

[URL=www.michaelsteven.net/atomic-kitten] atomic kitten [/URL]   <a href='www.michaelsteven.net/atomic-kitten'> atomic kitten </a>

# March 18, 2008 11:29 PM

... said:

[URL=www.michaelsteven.net/blocchi-autocad] blocchi autocad [/URL]   <a href='www.michaelsteven.net/blocchi-autocad'> blocchi autocad </a>

# March 19, 2008 6:08 AM

... said:

[URL=www.youngamericansfund.com/libri-italiano] libri italiano [/URL]   <a href='www.youngamericansfund.com/libri-italiano'> libri italiano </a>

# March 19, 2008 1:02 PM

... said:

[URL=www.myshots.info/ilaria-d-amico.php] Ilaria d amico [/URL]   <a href='www.myshots.info/ilaria-d-amico.php'> Ilaria d amico </a>

# March 20, 2008 8:10 AM

sandboxe said:

# May 28, 2008 11:19 PM

anil prasad said:

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?

Thanks,

Anil

# May 31, 2008 2:24 PM

boxandwh said:

ttp://www.esnips.com/user/arqpc866notf

# June 2, 2008 1:18 AM

Toronto oriental escorts said:

here's a good site for all of you!

# October 14, 2008 9:29 PM

accelpFes said:

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]

# April 10, 2009 11:21 AM

nick_trtroc said:

# May 17, 2009 5:20 PM

Messageheader custom | Yodetus said:

Pingback from  Messageheader custom | Yodetus

# January 6, 2012 4:43 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: