HttpHandlers and session state

While hacking on my website the other night I found a need to build an HttpHandler (actually a page would have worked fine, but a httphandler was more elegant). I got part way through debugging when I found that session state wasn’t available in an HttpHandler! This caused me some consternation as I have an HttpModule which I use to retrieve my nhibernate session during the execution of a request and it stores the nhibernate session in the HttpContext session (hopefully that’s not too many sessions and you’re still with me).

Anyway I almost re-engineered the handler to work another way when a search of google groups revealed this simple solution, your handler simply has to “implement” the empty IRequiresSessionState interface (you’ll need to be using the System.Web.WebSessionState namespace.) e.g.

public class VerificationHttpHandler : IHttpHandler, IRequiresSessionState

That’s it, you now have http session available in your handler.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s