views:

231

answers:

2

I'd like to use System.Web.Caching.Cache object in my application, but this code throws a NullReferenceException.

Cache cache = new Cache();
cache["key"] = new object();

Any ideas ?

Update

HttpRuntime.Cache solve my problem but does somebody has a solution to use multiple instance of cache in my application ?

+2  A: 

Use the HttpRuntime.Cache static property; it works fine.

Greg Beech
Thanks, but is there a solution to not use a level application cache ?
Nicolas Dorier
You mean to allow multiple instances of it in a single process? Not that I've found, no (although admittedly I haven't looked very hard as I only need one cache per process).
Greg Beech
Yes it's what I meant. It would be better with multiple instance because with this singleton unit tests are not independant anymore.
Nicolas Dorier
What we do to get around that is stick the ASP.NET cache behind a custom ICache interface, and then during unit tests you can use a quick and dirty mock implementation.
Greg Beech
yes good idea thanks :)
Nicolas Dorier
+2  A: 

Enterprise Library Caching Application Block might help you. It works fine for Windows applications as well as ASP.NET Applications.

Canavar