views:

114

answers:

2

Normally i have a static class that reads and writes to HttpContext.Current.Cache

However since adding threading to my project, the threads all get null reference exceptions when trying to retrieve this object.

Is there any other way i can access it, workarounds or another cache i can use?

+2  A: 

If your project is an ASP.NET project, then it was multithreaded even before you "added threading".

Check to see if HttpContext.Current is non-null before referencing HttpContext.Current.Cache.

Also, please post the complete exception you're receiving, and show us the code that references the cache.

John Saunders
+6  A: 

The System.Web.Cache object itself is thread safe.

The issue is how to obtain a reference to it in a way that works throughout your application. HttpContext.Current returns null unless it is called on a thread that is handling an ASP.NET request. An alternative way to get the Cache is through the static property System.Web.HttpRuntime.Cache. This will avoid the problems with the HttpContext.

binarycoder
HttpRuntime.Cache is the correct answer.
Chris
Oh cool, I don't know how I never came across HttpRuntime... +1, deleting my answer. :)
Tanzelax