Hacking of WCF's OperationContext

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

Sometimes you may require, that every message you send in context of calling of an operation, has some custom header. This should not be a big problem by implementing of messages on the messaging layer. However you may want to stay on the operation level (high-level) and manipulate the header of the message. The easiest way to do that is shown in the following example: 

MessageHeader sessionKeyHeader = MessageHeader.CreateHeader("SessionKey", "http://daenet.eu/SessionSharing", "Game1"); 

OperationContext.Current.OutgoingMessageHeaders.Add(sessionKeyHeader);

chn1.DoSomething();

This example shows how to append an additional header to the message which will be sent when the client invokes the operation DoSomething().

After the operation is invoked following message is sent.  

 <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:r="http://schemas.xmlsoap.org/ws/2005/02/rm" xmlns:a="http://www.w3.org/2005/08/addressing">

    <s:Header>
   
    <r:Sequence s:mustUnderstand="1">
   
        <r:Identifier>urn:uuid:294dfca8-11e6-41d7-acdd-79250f026a8d</r:Identifier>
   
        <r:MessageNumber>1</r:MessageNumber>
   
    </r:Sequence>
   
    <a:Action s:mustUnderstand="1">http://tempuri.org/IHello/DoSomething</a:Action>
   
    <SessionKey xmlns="
http://daenet.eu/SessionSharing">Game1</SessionKey>
   
    <a:MessageID>urn:uuid:a44f8afc-191d-4a39-bfcd-f1724c87169f</a:MessageID>
   
    <a:ReplyTo>
   
        <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
   
    </a:ReplyTo>
   
    <a:To s:mustUnderstand="1">http://localhost:7777/helloService/helloEndpoint</a:To>
   
    </s:Header>

<s:Body>

<DoSomething xmlns="http://tempuri.org/"></DoSomething>

</s:Body>

</s:Envelope>

Unfortinately this example will not work, because the OperationContext.Current is NULL reference. To avoid this problem following code can be used:  

ChannelFactory<HelloService.IHello> cnFactory = new ChannelFactory<HelloService.IHello>("helloClient");

HelloService.IHello chn1 = cnFactory.CreateChannel();

using (new OperationContextScope((IContextChannel)chn1))

{

MessageHeader sessionKeyHeader = MessageHeader.CreateHeader("SessionKey", "http://daenet.eu/SessionSharing", "Game1");

OperationContext.Current.OutgoingMessageHeaders.Add(sessionKeyHeader);

chn1.DoSomething();

}

This code creates the temporary OperationContext, which allows us to set the property OutgoingMessageHeaders. This is not nice solution, but it helps. Please note, that all calls to all operations whithin the scope defined by “using” will append the custom header with the name “SessionKey”. 

On the service side, the header can be found as shown: 

OperationContext.Current.RequestContext.RequestMessage.Headers.FindHeader(“SessionKey”, “http://daenet.eu/SessionSharing”)

 

 

 


Posted Oct 20 2006, 10:57 PM by Damir Dobric
Filed under:

Comments

Damir Dobric wrote re: Hacking of WCF's OperationContext
on 07-17-2008 12:15

Note that this does not work on Silverlight platform.

Damir Dobric Posts wrote Manipulating of Message Header in Silverlight
on 02-19-2009 23:44

Almost two years ago I have posted in " Hacking of WCF's OperationContext " about manipulating

Anjan Saha wrote re: Hacking of WCF's OperationContext
on 11-14-2009 8:11

It does work in the Silverlight Platform. I am able to capture the session key on the service side. Note this how the call will be at the service:

object i = OperationContext.Current.RequestContext.

               RequestMessage.Headers.GetHeader<string>(“SessionKey”, “daenet.eu/SessionSharing);

Damir Dobric wrote re: Hacking of WCF's OperationContext
on 11-14-2009 13:34

Hi Anjan,

I know that it does not work, because at the date of posting there was nor Silverlight at all (more or less) :)

Since few months this works in Silverlight too.

For more information take a look here:

developers.de/.../manipulating-of-message-header-in-silverlight.aspx

Damir

Anjan Saha wrote re: Hacking of WCF's OperationContext
on 11-25-2009 12:43

Ohh! yes..i was looking at Silverlight 2.0 onwards. You are right mate, the support was given then. BTW, did you think about  Token based security approach in Silverlight using this approach where you pass the token in the SOAP header.

Damir Dobric Posts wrote WCF: How to append header to HTTP Request?
on 06-04-2011 0:25

Long time ago I have described how to inject the message header in WCF message by using of Operation

Damir Dobric Posts wrote WCF: How to append header to HTTP Request?
on 06-04-2011 0:27

Long time ago I have described how to inject the message header in WCF message by using of Operation

DamirDobric wrote WCF: How to append header to HTTP Request?
on 06-04-2011 0:36

Long time ago I have described how to inject the message header in WCF message by using of Operation

developers.de is a .Net Community Blog powered by daenet GmbH.