【发布时间】:2015-02-23 19:17:30
【问题描述】:
我想统计我的用户,这是我的 dll 文件中的以下代码:
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();
}
}
我已设置Session.TimeOut=1;
一分钟后 Global.asax 文件中的以下方法将运行:
protected void Session_End(object sender, EventArgs e)
{
UserCount.subtract();
}
为什么subtract 方法中的HttpContext.Current null 会导致它抛出异常?
【问题讨论】:
-
请尽量让问题更清楚,包括检查拼写。
-
global.asax,session_end -
@AndersAbel 修复了它,还有什么剩下的吗?