Using Session within an HTTP Handler

6/23/2009

If you have ever created an HTTP handler, you've most likely run into the annoying fact that the session state is not accessible. In fact most of the time you're going to run into a null reference exception and will take you a couple of hours to figure out what the heck is going on. However hope is not lost as you can turn on the session state for a handler.

   1: public class MyHandler:IHttpHandler,IRequiresSessionState

Notice that last interface? That interface acts as a marker, letting the system know that the handler requires the session state for read and write access. We could just specify IReadOnlySessionState as well if we're only interested in reading the session state but not writing to it. So there you go, that's all that you need to do. So hopefully this helps someone out. Give it a try, leave feedback, and happy coding.



Comments