为什么在一段时间后删除对象

时间:2014-02-25 08:31:45

标签: c# class dictionary singleton durandal

我正在Durandal写一个应用程序。 (C#是服务器端,Javascript + HTML是客户端)。

在服务器端,我创建了一个CacheManager类。

我强迫这个类有一个单实例(单例)。在课堂上,我有一个名为cache的成员。这是保存我的应用程序数据的字典。当用户打开和关闭应用程序时,数据在运行时累积。

这是我的代码:

public class CacheManager
{
    #region Singleton
    // static holder for instance, need to use lambda to construct since constructor private
    private static readonly Lazy<CacheManager> _instance = new Lazy<CacheManager>(() => new CacheManager());

    // private to prevent direct instantiation.
    private CacheManager()
    {
        Cache = new Dictionary<string, object>();
    }

    // accessor for instance
    public static CacheManager Instance
    {
        get
        {

            return _instance.Value;
        }
    }

    public Dictionary<string, object> Cache { get; set; }

    #endregion
    //...continue application code
}

但是,如果从上次发送到服务器的请求开始已经很长时间了,那么在下次请求到达时,它会发现字典对象为空!

似乎c#中的对象具有有限的生命周期,当这个时间结束时,单例重新初始化,而字典接受一个新的空实例。

1 个答案:

答案 0 :(得分:3)

如果没有达到某个时间或内存限制的请求或只是一些超时,IIS将重新启动应用程序池进程。

相关问题