Why is HttpContext.Current null during the Session_End event?
Why is HttpContext.Current null during the Session_End event?
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.
Why is HttpContext.Current null during the Session_End event?的更多相关文章
- Why is HttpContext.Current null after await?
今天在对项目代码进行异步化改进的时候,遇到一个奇怪的问题(莫笑,以前没遇过),正如标题一样,HttpContext.Current 在 await 异步执行之后,就会变为 null. 演示代码: pu ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏
问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 350人阅读 评论(0) 收藏
问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...
- 多线程中使用HttpContext.Current为null的解决办法
HttpContext.Current.Server.MapPath(logFile) 这个是得到具体路径的方法 正常情况下是可以的 多线程情况下就为null 下边的代码原本的作用是把网站的异常 ...
- 我所知道的HttpContext.Current
在MVC中,HttpContext.Current是比较常见的对象,可以用它来进行Session,Cache等的保存等.但是它并不是无处不在的,下面就慢慢来揭开它的面纱. 当我们向服务端发送请求的时候 ...
- System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 未将对象引用设置为实例对象
做项目的时候,System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 获取路径本来这个方法用的好好的 因为需要 ...
- HttpContext.Current.Server.MapPath("/") 未将对象设置到对象的实例异常。
多线程中的System.Web.HttpContext.Current.Server.MapPath("/") 多线程中Server.MapPath会失效... 网上找到几种解决方 ...
- Web项目HttpContext.Current 为空
项目中,用到了WCF Service服务,用的是Windows身份验证,正常登陆后 HttpContext.Current=null 解决方法—— 1.在Web.config文件中添加配置项 < ...
- HttpContext.Current.Server.MapPath("") 未将对象设置到引用的
在多线程中使用该方法获取目录报错:未将对象设置到引用 #region 上传图片到腾讯 public async Task<Result> UploadImageToWX(string ba ...
随机推荐
- Value Iteration Algorithm for MDP
Value-Iteration Algorithm: For each iteration k+1: a. calculate the optimal state-value function for ...
- [Python3 练习] 008 欧几里德算法
题目:写个"欧几里德算法"的小程序 (1) 描述 我知识浅薄,一开始被"欧几里德"的大名唬住了,去搜了一下才知道这就是高中时学过的"辗转相除法&quo ...
- Git-第三篇廖雪峰Git教程学习笔记(2)回退修改,恢复文件
1.工作区 C:\fyliu\lfyTemp\gitLocalRepository\yangjie 2.版本库 我们使用git init命令创建的.git就是我们的版本库.Git的版本库里存了很多东西 ...
- jvm学习(2)JVM内存说明
前言 一.类方法 类方法是静态方法,前面需要有static修饰符修饰.类方法内不能涉及有关变量的内容1.不能调用类的对象方法2.不能引用对象变量3.类方法不能被重写(覆盖)4.类方法不能使用super ...
- EOJ Monthly 2019.2 A. 回收卫星
题目传送门 题意: 你可以询问一个三维坐标,机器会告诉你这个坐标在不在目标圆中, 并且(0,0,0)是一定在圆上的,叫你求出圆心坐标 思路: 因为(0,0,0)一定在圆上,所以我们可以把圆心分成3个坐 ...
- python内置函数sorted()及sort() 函数用法和区别
python内置函数sorted(),sort()都有排序的意思,但是两者有本质的区别,sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作,list 的 sort ...
- k8s结合helm部署
一.安装Helm helm教程以及安装可以参考这篇文章 二.Heml说明 常见的helm模板如下 myapp - chart 包目录名 ├── charts - 依赖的子包目录,里面可以包含多个依赖的 ...
- MATLAB:非线性规划fmincon
1.非线性规划的形式: 其中x是一个列向量,st中前两项为线性约束条件,后两项为非线性约束条件. 在MATLAB中fmincon是用于求解非线性多远函数的最小值的函数,这里介绍fmincon的其中一种 ...
- linux搭建tomcat集群+nginx
安装JDK 一.官方下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 下 ...
- Python之路-初识python及环境搭建与测试(Python安装、Anaconda安装、PyCharm安装)
一.认识Python 起源 Python的作者是著名的“龟叔”Guido van Rossum,他希望有一种语言,这种语言能够像C语言那样,能够全面调用计算机的功能接口,又可以像shell那样,可以轻 ...