HeartattacK

How to use Session values in an HttpHandler

When writing a custom HttpHandler, by default you have no access to the Session object. Doing something like HttpContext.Current.Session also returns null. The workaround is quite simple:

Reference the System.Web.SessionState namespace:

using System.Web.SessionState;

 ...and decorate the handler with either the IRequiresSessionState attribute:

public class MyHandler:IHttpHandler, IRequiresSessionState 

or the IReadOnlySessionState attribute:

public class MyHandler:IHttpHandler, IReadOnlySessionState

with the latter giving read only access to the seesion object.

 

Hope that helps.

 

EDIT: As pointed out...IReadOnlySessionState and IRequiresSessionState are not attributes, but empty interfaces. This is pretty apparent from the fact that we're not decorating the handler, rather the handler implements the interface [and since it's an empty interface, we don't need to implement anything to do so]. Late night blogging can lure the fingers to strange routes on the keyboard, it seems 8)

Posted: Sep 18 2008, 03:30 PM by HeartattacK | with 13 comment(s)
Filed under: , ,

Comments

funny wallpaper » How to use Session values in an HttpHandler said:

Pingback from  funny wallpaper » How to use Session values in an HttpHandler

# September 18, 2008 4:57 AM

Phil Bolduc said:

IRequiresSessionState and IReadOnlySessionState are not attributes, they are empty marker interfaces.

# September 18, 2008 12:23 PM

HeartattacK said:

Well yes...silly me...they are interfaces, not attributes. And they're empty, too. No method implementation needed to implement them.

# September 18, 2008 12:43 PM

rodrigo said:

This is not a workaround...Generic Handlers don't have Session access by default to be faster to load , but if you need sessions you can use these interfaces.

Its a feature, not a bug.

# September 18, 2008 12:58 PM

HeartattacK said:

I never said it was a bug. A workaround can "work around" a limitation. And in this case, the limitation is not being able to use Session in a handler by default. I'm not saying the approach is wrong. I'm just showing how somebody can access Session in a handler if they needed it.

# September 19, 2008 9:22 AM

Mafi Osori said:

Is anybody else bothered by this practice?

An interface isn't supposed to add behavior.  Even Microsoft defines interface correctly: "An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface" (msdn.microsoft.com/.../87d83y5b(VS.80).aspx).

By merely extending this interface we get more functionality ... something probably done through reflection in the base class to see if the interface is extended.

But if the behavior of the class is going to change from extending the interface, then we really are talking about multiple inheritance at that point.  Might as well call it a day and start developing in C++.

# October 8, 2008 9:57 AM

daman said:

Well guys, i have worked on handlers in vs2005 and have setup some of the session variables as well as redirected the handler to some pages as well so please tell me how to redirect from handler.

i am using

context.response.redirect(someurl)

it is giving error of object ref. not set

# November 11, 2008 6:49 AM

Thomas Watson said:

I am doing this correctly, but with a twist, I am starting a new thread in the handler and I need that thread to be able to retrieve a value from the session, basically, wait until the value is set from an AJAX call back in the browser... Any ideas? Or better yet, how can i wait for a value in the session to show up without stopping the async requests from processing. Doing Thread.Sleep seems to stop all the async requests from processing

# February 6, 2009 3:33 PM

Patrick said:

Thank you for this, I was looking on and off for 2 days how to do this.  For you VB.Net fellows, you can do this with an 'Implements IRequiresSessionState' line.

# March 26, 2009 11:10 AM

trendbender said:

thx, helpfull

# April 21, 2009 12:33 PM

mike_zandvliet_ said:

Thanks - very very helpful!

# August 6, 2009 4:12 AM

chriskuizon said:

Thanks very helpfull

# September 8, 2009 2:06 PM

David said:

Merci beaucoup, ça marche nickel.

# September 30, 2009 4:50 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)