DCSIMG
Using ASP.NET Cache in Non Web Applications - Doron's .NET Space

Using ASP.NET Cache in Non Web Applications

I'm a unit-testing newbie, but I'm trying to get into it (as I realized, one day, how scared I was to change working code someone else wrote a long time ago). Trying to test some of my code using NUnit failed completely, since that code tried to cache stuff, and the calls to HttpContext.Current.Cache threw a null reference exception. Well, silly me, of course that wouldn't work. There's no HttpContext when the unit test is running (it's not running in a web application), and therefore no Cache object. Or is there...?

Apparently you can use the ASP.NET cache in non web applications, as I found out thanks to this post. All you have to do is call HttpRuntime.Cache instead of HttpContext.Current.Cache and you're good to go. Of course, that will mean adding a reference to the System.Web assembly in my WinForms/Windows Service applications, which might be a bit awkward, but I guess I can look the other way in order to gain a great caching mechanism in non web applications, and a testable caching method in web applications.

Published Friday, February 16, 2007 8:42 PM by dorony
תגים:,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: