1

I want to count my users and this is the code below that is in my dll file:

public static class UserCount
{
    public static void add()
    {
        HttpContext.Current.Application.Lock();
        int count = (int) HttpContext.Current.Application["CountOfUsers"];
        count++;
        HttpContext.Current.Application["CountOfUsers"]=count;
        HttpContext.Current.Application.UnLock();
    }

    public static void subtract()
    {   
        HttpContext.Current.Application.Lock();//error : HttpContext.Current is null. why?
        int count = (int) HttpContext.Current.Application["CountOfUsers"];
        count--;
        HttpContext.Current.Application["CountOfUsers"]=count;
        HttpContext.Current.Application.UnLock();
    }
}

I have set Session.TimeOut=1; and after one minute the the method below in Global.asax file, this will run:

    protected void Session_End(object sender, EventArgs e)
    {
        UserCount.subtract();
    }

Why is HttpContext.Current null in the subtract method causing it to throw an exception?

rene
  • 41,474
  • 78
  • 114
  • 152
mohsen
  • 1,763
  • 3
  • 17
  • 55

1 Answers1

4

On Session_End there is no communication necessarily involved with the browser so there is no HttpContext to refer to which explains why it is null.

Looking at your code you seem to be intersted in the Application cache. That is available via Application property on the HttpApplication instance.

If you create an overload on your UserCount class that takes an HttpApplicationState you'll be fine:

public static void subtract(HttpApplicationState appstate)
{
    appstate.Lock();
    int count = (int) appstate["CountOfUsers"];
    count--;
    appstate["CountOfUsers"]=count;
    appstate.UnLock();
}

You can use this from Session_End like so:

protected void Session_End(object sender, EventArgs e)
{
    UserCount.subtract(Application);
}

This works because global_asax is technically an subclass from HttpApplication and so all its members are accessible from the global_asax file.

The other implementation of substract can be used when there is an HttpContext.

rene
  • 41,474
  • 78
  • 114
  • 152
  • Took a bit of digging to confirm this, but I believe you are correct. `HttpContext.Current` is dependent on execution context, which is different or missing when `Session_End` fires. Application state is still available (internally it's just a static member); the trick is to get to it as you've shown. Interestingly, I believe the OP's code would have worked in IIS 6; I recall doing something similar years ago with no problem. – Tim M. Jan 01 '15 at 21:42
  • 1
    Yeah, but... if Session_End is firing, shouldn't you have access to that session that's ending? To clean up the session that's ending... in the session end event? – Triynko Nov 23 '15 at 23:55
  • @Triynko yes, you can via [HttpApplication.Session](https://msdn.microsoft.com/en-us/library/system.web.httpapplication.session(v=vs.110).aspx). The problem of the OP was that they tried to access that Session by means of the HttpContext.Current which can be null. – rene Nov 24 '15 at 10:32