[英]How to cache in ASP.NET?
I have some commonly used data that I would like to load from my database and cache during Application_Start
in my global.asax file. 我有一些常用的数据,我想在我的global.asax文件中的Application_Start
期间从我的数据库和缓存加载。 I've been reading MSDN's article on caching , but I'm a bit confused on the proper way to do this. 我一直在阅读MSDN关于缓存的文章 ,但我对正确的方法有点困惑。
They example they give to insert data into cache is below: 他们将数据插入缓存的示例如下:
Cache.Insert("CacheItem2", "Cached Item 2");
So I added the following to my global.asax: 所以我在global.asax中添加了以下内容:
using System.Web.Caching;
...
Cache.Insert("audioVariables", audioVariables);
But this throws, An object reference is required....
. 但是抛出,需要An object reference is required....
Ok, fine - so I created an instance of the Cache class like so in Application_start
: 好的,很好 - 所以我在Application_start
创建了一个Cache类的实例:
Cache c = new Cache();
c.Insert("audioVariables", audioVariables);
When I call Insert
it throws a An unhandled exception of type 'System.NullReferenceException' occurred in System.Web.dll
error. 当我调用Insert
它会抛出An unhandled exception of type 'System.NullReferenceException' occurred in System.Web.dll
错误中An unhandled exception of type 'System.NullReferenceException' occurred in System.Web.dll
。
What is the proper way for me to insert an object into cache on Application_Start
? 在Application_Start
上将对象插入缓存的正确方法是什么?
UPDATE: 更新:
Stack trace - 堆栈跟踪 -
[NullReferenceException: Object reference not set to an instance of an object.] System.Web.Caching.Cache.Insert(String key, Object value) +66 MyVerbalInk.MvcApplication.Application_Start() in c:\\inetpub\\VerbalInk2.0\\MyVerbalInk\\Global.asax.cs:26 [NullReferenceException:对象引用未设置为对象的实例。] System.Web.Caching.Cache.Insert(String key,Object value)+66 MyVerbalInk.MvcApplication.Application_Start()in c:\\ inetpub \\ VerbalInk2.0 \\ MyVerbalInk \\的Global.asax.cs:26
[HttpException (0x80004005): Object reference not set to an instance of an object.] [HttpException(0x80004005):对象引用未设置为对象的实例。]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9935033 System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context,HttpApplication app)+9935033
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext,HttpContext context,MethodInfo [] handlers)+118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172 System.Web.HttpApplication.InitSpecial(HttpApplicationState状态,MethodInfo []处理程序,IntPtr appContext,HttpContext上下文)+172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext,HttpContext context)+336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)+296[HttpException (0x80004005): Object reference not set to an instance of an object.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9913572 [HttpException(0x80004005):对象引用未设置为对象的实例。] System.Web.HttpRuntime.FirstRequestInit(HttpContext context)+9913572
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)+101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext context)+254
You probably want to have a read of this article: 您可能希望阅读本文:
http://www.asp.net/web-forms/overview/data-access/caching-data/caching-data-at-application-startup-cs http://www.asp.net/web-forms/overview/data-access/caching-data/caching-data-at-application-startup-cs
However, the gist of the problem is that in II7 or greater running in Integrated mode, there is no HttpContext available in Application_Start(). 但是,问题的要点是在集成模式下运行的II7或更高版本中,Application_Start()中没有可用的HttpContext。 You have to use `HttpRuntime.Cache.Insert()' rather than Cache, or HttpContext.Current.Cache 你必须使用`HttpRuntime.Cache.Insert()'而不是Cache,或者HttpContext.Current.Cache
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.