简体   繁体   English

System.Web.Caching.Cache在模型中抛出null异常

[英]System.Web.Caching.Cache throws null exception in a model

Maybe this question should be easy, but it is not. 也许这个问题应该很容易,但事实并非如此。 I have read Problem Using the System.Web.Caching.Cache Class in ASP.NET . 在ASP.NET中阅读了使用System.Web.Caching.Cache类的问题

I have a singleton class: 我有一个单身人士课程:

private System.Web.Caching.Cache _cache;
private static CacheModel _instance = null;

private CacheModel() {
   _cache = new Cache();
}

public static CacheModel Instance {
         get {
            return _instance ?? new CacheModel();
         }
      }

public void SetCache(string key, object value){
   _cache.Insert(key, value);
}

If, anywhere else in my code, I call the following: 如果,在我的代码中的任何其他位置,我调用以下内容:

CacheModel aCache = CacheModel.Instance;
aCache.SetCache("mykey", new string[2]{"Val1", "Val2"});  //this line throws null exception

Why does the second line throw a null reference exception? 为什么第二行抛出空引用异常?

Maybe I have made some mistake somewhere in the code? 也许我在代码的某个地方犯了一些错误?

Thank you. 谢谢。

You shouldn't use the Cache type to initialise your own instance : 您不应使用Cache类型初始化您自己的实例

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code. 此API支持.NET Framework基础结构,不能直接在您的代码中使用。

Without looking directly into why you would get a null reference exception, and I've ran into this problem before, this is tied in with the infrastructure and lifecycle of a web application : 如果不直接了解为什么会出现空引用异常,并且我之前遇到过这个问题,那么它与Web应用程序的基础结构和生命周期紧密相关

One instance of this class is created per application domain, and it remains valid as long as the application domain remains active. 每个应用程序域创建一个此类的实例,只要应用程序域保持活动状态,它就仍然有效。 Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object. 有关此类实例的信息可通过HttpContext对象的Cache属性或Page对象的Cache属性获得。

Bottom line, don't use the System.Web.Caching.Cache type directly in this way - either access an existent cache instance or use an alternative like the Caching Application Block of the Enterprise Library . 最重要的是,不要以这种方式直接使用System.Web.Caching.Cache类型 - 访问现有的缓存实例或使用类似企业库缓存应用程序块的替代方案。

As Grant noted above the System.web.caching.cache stores ASP.NET internal data (like bundles that have been built in the App_start). 正如Grant在上面提到的, System.web.caching.cache存储了ASP.NET内部数据(就像在App_start中构建的bundle一样)。

Use the System.Runtime.Caching instead. 请改用System.Runtime.Caching here's the walkthrough on MSDN 这是MSDN上的演练

Here's a snippet: ` 这是一个片段:`

using System.Runtime.Caching;

...

ObjectCache cache = MemoryCache.Default;
var test = "hello world";
cache["greeting"] = test;
var greeting = (string)cache["greeting"];

` `

In ASP.NET 4, caching is implemented by using the ObjectCache class. 在ASP.NET 4中,使用ObjectCache类实现了缓存。 read more on MSDN 在MSDN上阅读更多内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM