[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

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?
0
AnOddProgrammer
Asked:
AnOddProgrammer
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now