please tell me why IHttpHandler need to implement IRequiresSessionState. without implementing it we can not read/write anything in session variable?
can’t we access directly HttpContext.Current.session like this way.
please explain…….thanks
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
var MyValue = context.Session["MyKey"] as String;
MyValue = "Hello World";
context.Session["MyKey"] = MyValue;
}
public bool IsReusable
{
get { return true; }
}
}
The session state is not a native part of the HTTP infrastructure. This means that the request will need to serialize/load the session information (either from memory or database or another server) for requests that need access to session and save it back when done.
HttpHandleris a the process (frequently referred to as the “endpoint”) that runs in response to a request made toASP.NET http runtime. It is used to handle a particular type of object/class/resource as you define it. If processing of that resource does not need access to session, that particular request does not need to go through the loading/saving of session data unnecessarily. So, by default, session is not available for HTTPhandlers, unless it is a predefined handler like Page handler.To successfully resolve any call to the Session object, the runtime environment must add the session state to the call context of the request being processed, which is
IRequireSessionStatein this case.Check out this msdn link for more details.
To answer your question, no, if you dont implement the
IRequireSessionState, you will not have access to the session objects in your handler class due to the above mentioned facts.