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?
LVL 1
AnOddProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rodney HelsensCommented:
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



0
AnOddProgrammerAuthor Commented:
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;
            }
        }
    }
}
0
Rodney HelsensCommented:
I've tested it locally, what I found that that the session variable .toString() is what is throwing the null reference exception error, putting this if statement around the code, fixes that error.

if (HttpContext.Current.Session["GoodLogin"] != null) {
 HttpContext.Current.Response.Write(HttpContext.Current.Session["GoodLogin"].ToString());
}

You may not be setting your session variable where you think you are. Try this:

create a page called SetSession.aspx and in the page_load put
Session["GoodLogin"] = "whatever";

in your web.config, set the httphandler to execute on all aspx pages like so

  <system.web>
  <httpHandlers>
  <add verb="*" path="*.aspx" type="FileHandler.SecureFileHandler, FileHandler" />
  </httpHandlers>


Now load up the SetSession.aspx, and refresh the page. you should see your session var on the screen when you refresh.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AnOddProgrammerAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.