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?