How to use Session values in an HttpHandler

1334 views

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)

Tags: .NET, ASP.NET

Sep 17 2008 11:30 PM

9 Comments

  • HeartattacK said

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

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

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

  • http:// 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" (http://msdn.microsoft.com/en-us/library/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++.

  • 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

  • http:// 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

  • http:// 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.

Add a Comment