多线程或者异步调用中如何访问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. Laravel 安装记录

    系统环境:ubuntu-14.10-server-i386(32) LAMP 系统默认安装 根据:http://laravel.com/docs/5.1  官方文档,准备通过 Composer 安装 ...

  2. MINA源码分析

    IoService通过构造函数的形式成为了IoSession一部分,IoSession是通过IoAcceptor以及connector创建出来,这二者其实就是IoService,所以对于IoSessi ...

  3. CoreGraphics --- 翻转坐标系

    1. 由于CoreGraphics 的坐标系与手机屏幕坐标系的Y轴是相反的, 所以在我们开发的时候, 需要翻转坐标系; - (void)drawRect:(CGRect)rect { CGContex ...

  4. Stanford CoreNLP--Split Sentence

    分句功能参考 Stanford Tokenizer. 在edu.stanford.nlp.pipeline包中实现了一系列分词分句功能,其中SentenceAnnotator类实现了对文件分句功能. ...

  5. 看了一下安装文件. 是qt4python 下用了 webkit,包装了bootstrap

    Pg9.6 安装包里的pgadmin4 反正软件是开源的,慢慢看源码呗.

  6. Spring 通过工厂方法(Factory Method)来配置bean

    Spring 通过工厂方法(Factory Method)来配置bean 在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. ...

  7. 【HDOJ】2851 Lode Runner

    开始没理解题意.原来destinations是指路的序号.而不是点.DP. #include <stdio.h> #include <string.h> ]; typedef ...

  8. C# WinForm捕获未处理的异常

    using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO; namespa ...

  9. SharePoint 2010中使用SPListItemCollectionPosition更快的结果

    转:http://www.16kan.com/article/detail/318657.html Introduction介绍 In this article we will explore the ...

  10. C# partial 局部类型

    关键字partial是一个上下文关键字,只有和 class.struct.interface 放在一起时才有关键字的含义.因此partial的引入不会影响现有代码中名称为partial的变量.局部类型 ...