Accessing the ASP.NET Cache from a Separate Thread?

前端 未结 2 1713
时光取名叫无心
时光取名叫无心 2021-01-17 11:08

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

2条回答
  •  遇见更好的自我
    2021-01-17 11:38

    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.

提交回复
热议问题