开发者

Regarding the usage of IRequiresSessionState

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 ProcessReques开发者_StackOverflow社区t(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.

HttpHandler is a the process (frequently referred to as the "endpoint") that runs in response to a request made to ASP.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 IRequireSessionState in 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.


If you do not implement this interface you should not be able to successfully use the session object. You are supposed to use this interface if you want access to the session, regardless of how you access it.

It shouldn't work without it. If it does, its a fluke and don't rely on it.


The ASP.NET SessionStateModule will not populate the context's session properties unless the handler is marked with the IRequiresSessionState interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜