Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# sessions "Object reference not set to an instance of an object."

I have an ASHX file:

Object reference not set to an instance of an object.

On the line:

HttpContext.Current.Session["loggedIn"] = true

Is this how I use sessions properly?

like image 604
Tom Gullen Avatar asked Jan 26 '11 13:01

Tom Gullen


1 Answers

I would guess that Session is the culprit here; with reference here, you might want to try adding : IRequiresSessionState to your handler (the code-behind for the ashx). So you should have something like:

public class Handler1 : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
        context.Session["loggedIn"] = true;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Note also that it is easier to talk to the context passed in, but HttpContext.Current should work too.

like image 159
Marc Gravell Avatar answered Sep 28 '22 06:09

Marc Gravell