C# ASP.NET: how to access cache when no HttpContext.Current is available (is null)?

Carl J. picture Carl J. · Dec 15, 2010 · Viewed 10.8k times · Source

During Application_End() in Global.aspx, HttpContext.Current is null. I still want to be able to access cache - it's in memory, so want to see if I can reference it somehow to save bits to disk.

Question - is there a way to reference cache in memory somehow when HttpContext.Current is null?

Perhaps I could create a global static variable that would store pointer to cache that I could update on HTTP requests (pseudo: "static <pointer X>" = HttpRequest.Current) and retrieve a reference to cache through that pointer in Application_End()?

Is there a better way to access Cache in memory when there is no Http Request is made?

Answer

James Gaunt picture James Gaunt · Dec 15, 2010

You should be able to access it via HttpRuntime.Cache

http://www.hanselman.com/blog/UsingTheASPNETCacheOutsideOfASPNET.aspx

According to Scott - looking at Reflector HttpContext.Current.Cache just calls HttpRuntime.Cache - so you might as well always access it this way.