ASP.NET多线程下使用HttpContext.Current为null解决方案
多线程或者异步调用中如何访问HttpContext?
前面我还提到在APM模式下的异步完成回调时,访问HttpContext.Current也会返回null,那么此时该怎么办呢?
答案有二种:
1. 在类型中添加一个字段来保存HttpContext的引用(异步开始前)。
2. 将HttpContext赋值给BeginXXX方法的最后一个参数(object state)
建议优先选择第二种方法,因为可以防止以后他人维护时数据成员被意外使用。
引用:
不错的文章:http://www.cnblogs.com/fish-li/archive/2013/04/06/3002940.html#_label5
http://bbs.csdn.net/topics/300139110
http://www.tuicool.com/articles/vYVziy
本文章转载:http://www.codesky.net/article/201004/103725.html
异步 HttpContext.Current实现取值的方法(解决异步Application,Session,Cache...等失效的问题)
回答的也多数都是:引用System.Web,不要用HttpContext.Current.Application应该用System.Web.HttpContext.Current.Application,后来在网上看到一篇关于System.Runtime.Remoting.Messaging.CallContext这个类的详细介绍才知道,原来HttpContext.Current是基于System.Runtime.Remoting.Messaging.CallContext这个类,子线程和异步线程都无法访问到主线程在CallContext中保存的数据。所以在异步执行的过程会就会出现HttpContext.Current为null的情况,为了解决子线程能够得到主线程的HttpContext.Current数据,需要在异步前面就把HttpContext.Current用HttpContext的方式存起来,然后能过参数的形式传递进去,下面看看实现的方法:
{
get { return HttpContext.Current; }
set { value = context; }
}
然后建立一个委托
下面就是实现过程的编码
{
context = HttpContext.Current;
delegategetResult dgt = testAsync;
IAsyncResult iar = dgt.BeginInvoke(context, null, null);
string result = dgt.EndInvoke(iar);
Response.Write(result);
}
public static string testAsync(HttpContext context)
{
if (context.Application["boolTTS"] == null)
{
Hashtable ht = (Hashtable)context.Application["TTS"];
if (ht == null)
{
ht = new Hashtable();
}
if (ht["A"] == null)
{
ht.Add("A", "A");
}
if (ht["B"] == null)
{
ht.Add("B", "B");
}
context.Application["TTS"] = ht;
}
Hashtable hts = new Hashtable();
hts = (Hashtable)context.Application["TTS"];
if (hts["A"] != null)
{
return "恭喜,中大奖呀";
}
else
{
return "我猜你快中奖了";
}
}
ASP.NET多线程下使用HttpContext.Current为null解决方案的更多相关文章
- 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,解决办 ...
- ASP.NET多线程下使用HttpContext.Current
本来要实现asp.net下使用tcp通讯方式向服务器获取数据,开始采用的方式是 参考: ASP.NET多线程下使用HttpContext.Current为null解决方案 http://www.cnb ...
- 多线程中使用HttpContext.Current为null的解决办法
HttpContext.Current.Server.MapPath(logFile) 这个是得到具体路径的方法 正常情况下是可以的 多线程情况下就为null 下边的代码原本的作用是把网站的异常 ...
- 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法
在项目中使用异步(async await)的时候发现一个现象,HttpContext.Current为null,导致一系列的问题. 上网查了一些资料后找到了一个对象: System.Threading ...
- .net webapi 中使用session是出错 HttpContext.Current.Session==null
最近在写.net webapi时发现 HttpContext.Current.Session==null ,导致报错,后来查资料发现webapi中使用session时首先需要开启session功能, ...
- http流请求时,被请求站点HttpContext.Current为null?
我负责运维一个短信接口站点sms.调用上游短信供应商下发短信后,他们会给我们推送发送报告.报告是类似DELIVRD.DI:9432这样的码.为了方便识别,系统里有一个报告码与其描述的关系,一开始是写死 ...
- 异步任务,HttpContext.Current为null解决办法
最近在开发一个后台管理系统项目,为了提高登录的速度,就把记录登录日志放到一个异步任务里面. Action taskAction = () => { SaveLog(); }; Task task ...
- HttpContext.Current为NULL
总结:HttpContext.Current是基于System.Runtime.Remoting.Messaging.CallContext这个类,子线程和异步线程都无法访问到主线程在CallCont ...
随机推荐
- IIS日志分析
发现一个强大的图形化IIS日志分析工具——Log Parser Studio,下面分享一个实际操作案例. 1. 安装Log Parser Studio a) 需要先安装Log Parser,下载地址: ...
- VS2013发布web项目到IIS上遇到的问题总结
vs2010发布网站到本地IIS的步骤 http://blog.csdn.net/cx_wzp/article/details/8805365 问题一:HTTP 错误 403.14 - Forbid ...
- cursor_sharing
CURSOR_SHARING Property Description Parameter type String Syntax CURSOR_SHARING = { EXACT | FORCE } ...
- ubuntu下SVN服务器安装配置
SVN服务器端: 1.安装包 $ sudo apt-get install subversion 2.添加svn管理用户及subversion组 $ sudo adduser svnuser $ su ...
- Word2003中如何使封面和目录中不插入页码
Word2003中如何使封面和目录中不插入页码?? 转载自: http://blog.zzedu.net.cn/user1/zhaoweijie/archives/2010/187266.html ...
- 【技术贴】解决QQ空间发表文章手机不显示换行
采用HTML模式,在需要换行的地方加入如下代码. <div><span style="font-family:微软雅黑;font-size:16px"> & ...
- Lea指令计算地址(用于四则混合运算),附上一个函数调用例子及其反汇编代码,很清楚
比如你用local在栈上定义了一个局部变量LocalVar,你知道实际的指令是什么么?一般都差不多像下面的样子: push ebp mov esp, ebp sub ...
- System.Windows.Forms中的Message Structure
结构用途说明Implements a Windows message. Properties 1.public IntPtr HWnd { get; set; } Gets or sets the w ...
- ImageSwitcher 右向左滑动的实现方式
ImageSwitcher is;...is.setInAnimation(this, android.R.anim.slide_in_left);is.setOutAnimation(this, a ...
- Hay Points
Hay Points TimeLimit: 1 Second MemoryLimit: 32 Megabyte Totalsubmit: 1022 Accepted: 602 Descript ...