从一个单独的线程访问ASP.NET缓存? [英] Accessing the ASP.NET Cache from a Separate Thread?
问题描述
通常,我有一个静态类,读取和写入 HttpContext.Current.Cache
Normally i have a static class that reads and writes to HttpContext.Current.Cache
不过,由于添加线程到我的项目,试图检索此对象的时候,线程都得到空引用异常。
However since adding threading to my project, the threads all get null reference exceptions when trying to retrieve this object.
有没有其他办法可以访问它,解决方法或其他高速缓存可以使用吗?
Is there any other way i can access it, workarounds or another cache i can use?
推荐答案
的 System.Web.Cache
对象本身是线程安全的。
The System.Web.Cache
object itself is thread safe.
问题是如何在整个应用程序,工作方式获得对它的引用。 HttpContext.Current
,除非它被称为一个线程正在处理ASP.NET请求返回null。另一种方式来获得缓存
是通过静态属性 System.Web.HttpRuntime.Cache
。这将避免问题的的HttpContext
。
The issue is how to obtain a reference to it in a way that works throughout your application. HttpContext.Current
returns null unless it is called on a thread that is handling an ASP.NET request. An alternative way to get the Cache
is through the static property System.Web.HttpRuntime.Cache
. This will avoid the problems with the HttpContext
.
这篇关于从一个单独的线程访问ASP.NET缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!