.net - How can I use System.Web.Caching.Cache in a Console application? -
Reference: .Net 3.5, C #
I want to see the caching mechanism in my console application.
Instead of searching the wheel, I would like to use the System.Web.Caching.Cache
(and this is the final decision, I can not use other caching frameworks, why not ask ).
However, it seems that System.Web.Caching.Cache
should only be run in a valid HTTP context. My very simple snippet looks like this:
using the system; Using System.Web.Caching; Using System.Web; Cache c = new cache (); Try {c.Insert ("a", 123); } Hold (Exception Pre) {Console.WriteLine ("Can not be inserted to the cache, exception:"); Console.WriteLine (ex); }
And the result is:
The cache can not be included in the exception: System.NullReferenceException: Object is not set to an instance of reference object . On MyWeb.Caching.Cache.Instert (string key, object value) on MyClass.RunSnippet ()
So obviously, I'm doing something wrong here, any thoughts?
HttpContext.Current.Cache
. thanks all!
The document for the cash constructor says that this is only for internal use. To get your cache object, call HttpRuntime.Cache instead of making an example through Constructor.
Comments
Post a Comment