4

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 Answers 2

4

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

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

4 Comments

Thanks, but is there a solution to not use a level application cache ?
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).
Yes it's what I meant. It would be better with multiple instance because with this singleton unit tests are not independant anymore.
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.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.