多线程或者异步调用中如何访问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. Jersey的异常处理

    Jersey框架为我们提供了更为通用异常处理方式.通过实现ExceptionMapper接口并使用@Provider注解将其定义为一个Provider,可以实现通用的异常的面向切面处理,而非针对某一个 ...

  2. css实现网页布局随滚轮变化响应移动

    _position:absolute; _top:expression(eval(document.documentElement.scrollTop)); 1.第一句代码 _position:abs ...

  3. ipad在非viewport 1:1下缩放问题

    1.最小会有980宽度,小于980应设置viewport 2.fix元素使用100%指定宽度时,默认会以min-width或980作为尺寸,可以选择给定与页面缩放时触发定宽来设置宽度,或设置设置bod ...

  4. A simple test

        博士生课程报告       视觉信息检索技术                 博 士 生:施 智 平 指导老师:史忠植 研究员       中国科学院计算技术研究所   2005年1月   目 ...

  5. Hbase案例分析(二)

    情景1:

  6. Codeforces Round #197 (Div. 2) : A

    水题一个: 直接贴代码: #include<cstdio> #include<algorithm> #include<cstring> using namespac ...

  7. The APR based Apache Tomcat Native library

    Tomcat启动的时候出现下面这样的提示: 2015-11-06 14:24:12 org.apache.catalina.core.AprLifecycleListener init 信息: The ...

  8. AFN演示

  9. Android开源项目发现---Layout 篇(持续更新)

    1. achartengine 强大的图标绘制工具 支持折线图.面积图.散点图.时间图.柱状图.条图.饼图.气泡图.圆环图.范围(高至低)条形图.拨号图/表.立方线图及各种图的结合 项目地址:http ...

  10. Android 应用页面延缓载入

    1.新建一个线程,使用handle的延缓运行线程 new Handler().postDelayed(new Runnable() { // 为了减少代码使用匿名Handler创建一个延时的调用 pu ...