我如何获取HttpContext的属性时,它返回null? [英] How do I retrieve the HttpContext properties when it returns null?

查看:91
本文介绍了我如何获取HttpContext的属性时,它返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用做一个单独的线程异步一些工作:

I am doing some asynchronous work on a separate thread using:

ThreadPool.QueueUserWorkItem()

在这个单独的线程,我需要调用 HttpContext.Current ,这样我可以访问:

HttpContext.Current.Cache  
HttpContext.Current.Server  
HttpContext.Current.Request  

然而, HttpContext.Current 当我创建这个单独的线程为null。

However, HttpContext.Current is null when I create this separate thread.

如何创建一个新的线程,这样 HttpContext.Current 不为空?还是有另一种方法,我可以访问缓存,服务器和请求对象?

How do I create a new thread so that HttpContext.Current is not null? Or is there another way I can access the Cache, Server, and Request objects?

推荐答案

我会尽量不坚持取决于像HttpContext的ASP.NET的堆栈上的对象的引用。如果你需要做的在不同的线程一些工作,那是因为你不希望在ASP.NET之一,等到你的任务就完成了。也许当你的线程不是请求/上下文/会话将被终止。

I'd try not to hold a reference to an object that depends on the ASP.NET stack like the HttpContext. If you need to do some work in a different thread, it's because you don't want to wait in the ASP.NET one till your task is finished. And maybe the Request/Context/Session is terminated while your thread is not.

您应该通过与需要为您的线程中的数据的对象。

You should pass an object with the data needed for your thread.

这篇关于我如何获取HttpContext的属性时,它返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆