Link to home
Start Free TrialLog in
Avatar of AnOddProgrammer
AnOddProgrammer

asked on

HttpHandler gets 'Object reference not set to an instance of an object' when trying to access session vars.

Im programing a http handler in c# 2005 beta, and i need to access session vars, but when I tried to access them through the httpContext.Session i got the error 'Object reference not set to an instance of an object'.  I already tried useing System.Web.SessionState.IRequiresSessionState and System.Web.HttpContext.Current.Session, but neither of these worked.  Does anyone know how to fix this?
Avatar of Rodney Helsens
Rodney Helsens

If you are using the default, InProc session state, the session vars will be lost each time the aspnet process recycles. This may be your problem, if you post your code, I will try it on my machine to see if it works for me. are you able to use session variables on other pages.?

while this KB alert isn't necessarily your problem, it may give you something to go on, to investigate the aspnet worker process...
http://www.kbalertz.com/kb_841557.aspx



Avatar of AnOddProgrammer

ASKER

Here is the code to the HTTP handler

namespace FileHandler
{
    //By directory file security
    public class SecureFileHandler : System.Web.SessionState.IRequiresSessionState, IHttpHandler
    {

        //Contructor
        public SecureFileHandler()
        {
        }

        //IHttpHandler Interface
        //Process Request
        public void ProcessRequest(System.Web.HttpContext context)
        {
            //These lines cause an error
            context.Response.Write(context.Session["GoodLogin"].ToString());
            context.Response.Write(System.Web.HttpContext.Current.Session["GoodLogin"].ToString());
           
            //This line causes no error
            context.Response.Write("Test");
        }

        //IsReusable Var
        public bool IsReusable
        {
            get
            {
                 return true;
            }
        }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of Rodney Helsens
Rodney Helsens

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thankyou for your help.  The reson that it wasnt working was because the session vars had expired without me relizing it, now it works. Thanks again.