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?的更多相关文章

  1. Why is HttpContext.Current null after await?

    今天在对项目代码进行异步化改进的时候,遇到一个奇怪的问题(莫笑,以前没遇过),正如标题一样,HttpContext.Current 在 await 异步执行之后,就会变为 null. 演示代码: pu ...

  2. ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏

    问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...

  3. ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 350人阅读 评论(0) 收藏

    问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...

  4. 多线程中使用HttpContext.Current为null的解决办法

    HttpContext.Current.Server.MapPath(logFile)   这个是得到具体路径的方法  正常情况下是可以的 多线程情况下就为null 下边的代码原本的作用是把网站的异常 ...

  5. 我所知道的HttpContext.Current

    在MVC中,HttpContext.Current是比较常见的对象,可以用它来进行Session,Cache等的保存等.但是它并不是无处不在的,下面就慢慢来揭开它的面纱. 当我们向服务端发送请求的时候 ...

  6. System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 未将对象引用设置为实例对象

    做项目的时候,System.Web.HttpContext.Current.Server.MapPath("~/upload/SH")   获取路径本来这个方法用的好好的 因为需要 ...

  7. HttpContext.Current.Server.MapPath("/") 未将对象设置到对象的实例异常。

    多线程中的System.Web.HttpContext.Current.Server.MapPath("/") 多线程中Server.MapPath会失效... 网上找到几种解决方 ...

  8. Web项目HttpContext.Current 为空

    项目中,用到了WCF Service服务,用的是Windows身份验证,正常登陆后 HttpContext.Current=null 解决方法—— 1.在Web.config文件中添加配置项 < ...

  9. HttpContext.Current.Server.MapPath("") 未将对象设置到引用的

    在多线程中使用该方法获取目录报错:未将对象设置到引用 #region 上传图片到腾讯 public async Task<Result> UploadImageToWX(string ba ...

随机推荐

  1. 解决django项目在ubuntu系统上无法安装mysqlclient

    首先我的项目是django2.0,python环境是3.5. 我们在本地开发完django项目了,在本地运行是成功的,然后我们把django项目放到服务器上,运行的时候就出错了. 如图: 我们都知道, ...

  2. python中pycharm中.py文件调用一个.py文件的函数

    在相同文件夹内调用函数: file1.py def add(x,y): print('和为:%d'%(x+y)) file2.py import A A.add(1,2)

  3. Elasticsearch安装及遇到的问题(CentOS 7.3 64位)

    Elasticsearch安装 使用root用户创建一个其他用户,(elasticsearch不能在root账户下安装) # 添加一个名字是es工作组 groupadd es # 添加用户es设置密码 ...

  4. HDFS中NameNode和Secondary NameNode工作机制

    NameNode工作机制 0)启动概述 Namenode启动时,首先将映像文件(fsimage)载入内存,并执行编辑日志(edits)中的各项操作.一旦在内存中成功建立文件系统元数据的映像,则创建一个 ...

  5. IDEA创建SpringBoot+maven项目

    1.创建项目: 2.选择spring Initializr,注意要选择jdk,使用默认的spring.io这样就不用再去写pom文件了 3.输入项目名称: 4.选择Spring Web 5.目录结构:

  6. 三连击 P1008 洛谷 python写法

    三连击 P1008 洛谷 题意 将\(1,2, \cdots,9\)共9个数分成3组,分别组成3个三位数,且使这3个三位数构成1:2:3的比例,试求出所有满足条件的3个三位数. 解题思路 这里我使用了 ...

  7. AcWing 802. 区间和

    (https://www.acwing.com/problem/content/804/) 假定有一个无限长的数轴,数轴上每个坐标上的数都是0. 现在,我们首先进行 n 次操作,每次操作将某一位置x上 ...

  8. 百度文档,用Python一键免费下载

    百度文库下载需要券,或者vip才能下载 Vip价格高,偶尔下载一次不划算. 不下载复制?不好意思复制也需要vip否则只能一次复制两行. 如何才能以最低成本获取到百度文库里的文档内容呢? 当然是用Pyt ...

  9. expdp和impdp的应用-高版本通过dblink导入到低版本

    今天接到要进行数据库用户的部分数据迁移需求,需求如下 IMP.WO.INSA开头的表只要结构,不要数据 B.TEMP.TMP开头的表不用导 其他表需要导出数据和表结构,同时要求导出此用户下的所有其他对 ...

  10. 什么是http协议??

    一.http协议的定义: http(Hypertext transfer protocol)超文本传输协议,通过浏览器和服务器进行数据交互,进行超文本(文本.图片.视频等)传输的规定.也就是说,htt ...