asp.net - 返回null时,如何检索HttpContext属性?

标签 asp.net httpcontext

我正在使用以下命令在单独的线程上进行一些异步工作:

ThreadPool.QueueUserWorkItem()
在这个单独的线程中,我需要调用HttpContext.Current以便访问:
HttpContext.Current.Cache  
HttpContext.Current.Server  
HttpContext.Current.Request  
但是,当我创建此单独的线程时,HttpContext.Current为null。

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

最佳答案

我尽量不要保留对依赖于ASP.NET堆栈(如HttpContext)的对象的引用。如果您需要在其他线程中进行某些工作,那是因为您不想在ASP.NET中等待任务完成。也许请求/上下文/ session 被终止而您的线程没有被终止。

您应该将对象与线程所需的数据一起传递。

关于asp.net - 返回null时,如何检索HttpContext属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/529286/

相关文章:

ASP.NET MVC 包罗万象的路由

asp.net - Web 应用程序最方便的阿拉伯字体

c# - 视频文件的 GET 请求已取消。找不到视频

c# - HttpContext 类及其线程安全

asp.net-core - Asp.Net Core 中的 HttpContext.Features 与 HttpContext.Items

asp.net - 更改 WCF System.ServiceModel.ClientBase 的 Endpoint.Binding 不起作用

c# - IIS Express 路由在 Visual Studio 2019 中无法正常工作

c# - Url Referrer 在 WebApi 2 MVC 项目中不可用

cache-control - 什么是设置无缓存 ="Set-Cookie"