3
votes

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

1
There is no way for c to come back null in the code you posted.John Saunders
Sorry - you're right. Updated.Abe Miessler
Please show the full stack trace. Even though it's in System.Web, it may give us a clue.John Saunders
That makes me wonder if audiovariables isn't null. Still shouldn't NRE, but... can you check the source code of Cache.Insert?John Saunders
See blogs.msdn.com/b/dotnet/archive/2014/02/24/… for info and referencesource.microsoft.com in particular, though I don't see this namespace in there.John Saunders

1 Answers

5
votes

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

However, the gist of the problem is that in II7 or greater running in Integrated mode, there is no HttpContext available in Application_Start(). You have to use `HttpRuntime.Cache.Insert()' rather than Cache, or HttpContext.Current.Cache