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. Spring Boot 中的 Tomcat 是如何启动的?

    作者:木木匠 https://my.oschina.net/luozhou/blog/3088908 我们知道 Spring Boot 给我们带来了一个全新的开发体验,让我们可以直接把 Web 程序打 ...

  2. [BNDSOJ] #1106代码

    #include<bits/stdc++.h> using namespace std; ]; ][]; int n; bool check(int i,int j) { ]==]==]= ...

  3. HDU-1873 看病要排队(队列模拟)

    看病要排队 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  4. 问题 E: Jack的A+B

    问题 E: Jack的A+B 时间限制: 1 Sec  内存限制: 128 MB提交: 1996  解决: 601[提交] [状态] [命题人:jsu_admin] 题目描述 现在有整数a,b,请按西 ...

  5. RequireJS 入门(二)

    简介 如今最常用的JavaScript库之一是RequireJS.最近我参与的每个项目,都用到了RequireJS,或者是我向它们推荐了增加RequireJS.在这篇文章中,我将描述RequireJS ...

  6. 解决本地mysql服务允许被外部主机连接

    今天在网上百度看了怎么使用外部主机连接本地MySQL服务,发现大多的说法都是不全面的,试了好久,整理下: 1.现创建了一个mysql用户,并赋予常用的操作权限 CREATE USER 'mysql'@ ...

  7. sftp没有关闭session导致服务器sshd进程未关闭

    项目中需要用Sftp上传下载文件,通过jsch中的sftp实现.代码上了服务器之后,发觉服务器多了很多进程没有被关闭. 连接sftp代码: protected boolean connectToSer ...

  8. Docker环境下的前后端分离项目部署与运维

    本教程将从零开始部署一个前后端分离的开源项目,利用docker虚拟机的容器技术,采用分布式集群部署,将项目转换成为高性能.高负载.高可用的部署方案.包括了MySQL集群.Redis集群.负载均衡.双机 ...

  9. 联想ideapad 310s如何进BIOS,换固态硬盘SSD,配置U盘启动,重装Win10系统

    1. 如何进BIOS 关机情况下,捅一下Novo键,即可进入BIOS 2. 安装固态硬盘 Ideadpad 310S 本身自带的硬盘是5400转的机械硬盘,容量小速度慢.换的新的固态硬盘是SATA接口 ...

  10. TensorFlow学习笔记2:逻辑回归实现手写字符识别

    代码比较简单,没啥好说的,就做个记录而已.大致就是现建立graph,再通过session运行即可.需要注意的就是Variable要先初始化再使用. import tensorflow as tf fr ...