从一个单独的线程访问ASP.NET缓存? [英] Accessing the ASP.NET Cache from a Separate Thread?

查看:139
本文介绍了从一个单独的线程访问ASP.NET缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我有一个静态类,读取和写入 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屋!

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