I was trying to access session inside ASHX page but I was getting a null reference exception. I think session should be available on every request but it’s not the case with ASHX handler. I was wrong there.
If you look at the ASHX page, it implements IHttpHandler interface. To be able to access session it needs to implement IReadOnlySessionState or IRequiresSessionState. But you may be wondering how ASPX page be able to access session even though it doesn’t implements IReadOnlySessionState or IRequiresSessionState. Any idea ?? The answer is left to reader. Please leave a comment if you are eager to know that.
So that part was missing in case of ASHX page handler. You should implement IReadOnlySessionState or IRequireSessionState depends on what you need to do with session.
- User IReadOnlySessionState if you only need to read values from Session.
//reading value from session string value = Session["Key"].ToString();
- User IRequireSessionState if you need to store values in Session.
//store some value in session Session["Key"] = value;
Want to know more about Session? You may refer articles that I posted earlier
Hope this helps,