3

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

8
  • 1
    There is no way for c to come back null in the code you posted. Commented Dec 19, 2014 at 23:23
  • Sorry - you're right. Updated. Commented Dec 19, 2014 at 23:25
  • Please show the full stack trace. Even though it's in System.Web, it may give us a clue. Commented Dec 19, 2014 at 23:27
  • That makes me wonder if audiovariables isn't null. Still shouldn't NRE, but... can you check the source code of Cache.Insert? Commented Dec 19, 2014 at 23:31
  • 1
    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. Commented Dec 19, 2014 at 23:37

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Erik, out of curiosity, how would the lack of HttpContext cause the stack trace that was posted? That's very early in the Insert method, and is not inside of another method.
@JohnSaunders - I belive his stack trace is from when he instantiated a Cache object, which to be honest, no idea which Cache object he created. From what I can see, the System.Web.Caching.Cache class is not intended to be created by itself, so who knows what might actually be happening. I'm not motivated enough to check the sources...
@JohnSaunders in fact, the doc header for System.Web.Caching.Cache for the constructor says "This constructor is for internal use only, and was accidentally made public - do not use."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.