Archive, ASP.NET
     

Enabling Session State In IHttpHandlers

Sometimes it is handy to have the HttpSession available from your custom IHttpHandlers. By default the session state is unavailable from IHttpHandlers, and attempting to access it will result in a NullReferenceException. To make session state available simply implement the IRequireSessionState interface.

using System.Web;
using System.Web.SessionState;

public class SomeHandler : IHttpHandler, IRequiresSessionState {

     public void ProcessRequest(HttpContext context){
          //do processing that requires session state.
	  //use the context.Session to acquire the session state.
     }

     public bool IsReusable {
          get { return false; }
     }
}

The IRequiresSessionState interface is simply a marker interface, thus it has no members to implement. Once implemented your IHttpHandler can acquire the session state using the Session property of the HttpContext parameter on the ProcessRequest method.

Never miss an article! Subscribe to my newsletter and I'll keep you updated with the latest content.

 

About Jason

Jason is an experienced entrepreneur & software developer skilled in leadership, mobile development, data synchronization, and SaaS architecture. He earned his Bachelor of Science (B.S.) in Computer Science from Arkansas State University.
View all posts by Jason →

Leave a Reply

Your email address will not be published. Required fields are marked *