多线程或者异步调用中如何访问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的方式存起来,然后能过参数的形式传递进去,下面看看实现的方法:

复制代码代码如下:
public HttpContext context 

get { return HttpContext.Current; } 
set { value = context; } 

然后建立一个委托

复制代码代码如下:
public delegate string delegategetResult(HttpContext context); 

下面就是实现过程的编码

复制代码代码如下:
protected void Page_Load(object sender, EventArgs e) 

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解决方案的更多相关文章

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

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

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

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

  3. ASP.NET多线程下使用HttpContext.Current

    本来要实现asp.net下使用tcp通讯方式向服务器获取数据,开始采用的方式是 参考: ASP.NET多线程下使用HttpContext.Current为null解决方案 http://www.cnb ...

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

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

  5. 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法

    在项目中使用异步(async await)的时候发现一个现象,HttpContext.Current为null,导致一系列的问题. 上网查了一些资料后找到了一个对象: System.Threading ...

  6. .net webapi 中使用session是出错 HttpContext.Current.Session==null

    最近在写.net webapi时发现 HttpContext.Current.Session==null  ,导致报错,后来查资料发现webapi中使用session时首先需要开启session功能, ...

  7. http流请求时,被请求站点HttpContext.Current为null?

    我负责运维一个短信接口站点sms.调用上游短信供应商下发短信后,他们会给我们推送发送报告.报告是类似DELIVRD.DI:9432这样的码.为了方便识别,系统里有一个报告码与其描述的关系,一开始是写死 ...

  8. 异步任务,HttpContext.Current为null解决办法

    最近在开发一个后台管理系统项目,为了提高登录的速度,就把记录登录日志放到一个异步任务里面. Action taskAction = () => { SaveLog(); }; Task task ...

  9. HttpContext.Current为NULL

    总结:HttpContext.Current是基于System.Runtime.Remoting.Messaging.CallContext这个类,子线程和异步线程都无法访问到主线程在CallCont ...

随机推荐

  1. makefile debug

    1. 使用warning指令 warning 是个不错的命令,可以打印出消息,来判断makefile执行的流程 2.使用ifeq ifneq 当makefile被多次调用到的时候,如果都输出warni ...

  2. iis post 请求.html文件报405

    其实本地文件默认是不允许post请求的,但是需要配置一下,配置如下: 我的iis版本是8.5             当然默认也是不能post请求  *.html或是*.json的的文件的,这个问题困 ...

  3. python面向对象高级编程

    正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: >>> class Studen ...

  4. 《转》SQL Server 2008 数据维护实务

    SQL Server 2008 数据维护实务 http://blog.csdn.net/os005/article/details/7739553 http://www.cnblogs.com/xun ...

  5. 内存卡的class是什么意思 内存卡class的5个等级

    内存卡的class是什么意思 SD协会针对TF内存卡(SDHC)的传输规范:传输速度(写入速度及读取速度的最低速度)被定义为Class2(2MB/sec).Class4(4MB/sec).Class6 ...

  6. 程序异常捕获库 - CrashRpt

    CrashRpt.dll用来在应用程序出现异常crash时,捕获到错误. 并收集出错信息: MiniDump文件.硬件信息.系统信息.出错信息.进程信息.服务信息.驱动信息.启动信息.软件列表.端口信 ...

  7. Php使用sqlite

    php sqlite文档:http://php.net/manual/en/book.sqlite.php sql:http://www.php100.com/html/webkaifa/PHP/PH ...

  8. PEP Index > PEP 339 -- Design of the CPython Compiler 译文

    http://www.python.org/dev/peps/pep-0339/ PEP: 339 标题: CPython的编译器设计 版本: 425fc5598ee8 最后修改: 2011-01-1 ...

  9. POJ_2001_Shortest_Prefixes_(Trie)

    描述 http://poj.org/problem?id=2001 给出一组单词,求每个单词的最小唯一前缀. 最小唯一前缀:该前缀不能是其他单词的前缀,并且最小,如果不存在,则为该单词本身. Shor ...

  10. 段错误调试神器 - Core Dump详解

    一.前言: 有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的. 但这不像编译错误一样会提示到文件某一行, 而是没有任何信息, 使得我们的调试变得 ...