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. I've been reading MSDN's article on caching, but I'm a bit confused on the proper way to do this.
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:
using System.Web.Caching;
...
Cache.Insert("audioVariables", audioVariables);
But this throws, An object reference is required....
. Ok, fine - so I created an instance of the Cache class like so in Application_start
:
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.
What is the proper way for me to insert an object into cache on 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
[HttpException (0x80004005): Object reference not set to an instance of an object.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9935033
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.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
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
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
c
to come back null in the code you posted. – John Saundersaudiovariables
isn't null. Still shouldn't NRE, but... can you check the source code of Cache.Insert? – John Saunders