简体   繁体   English

一个WebApplication有2个Singleton类的实例

[英]A WebApplication has 2 instances of Singleton class

I have a singleton class where a list of objects is placed. 我有一个单例类,其中放置了对象列表。 So I have a class that sets items to that list. 因此,我有一个将项目设置为该列表的类。 Then, I have another class that gets the list of objects from that singleton class. 然后,我有另一个类从该单例类获取对象列表。 My issue is when I receive an nservicebus message and get the list from the singleton class, there are times that the list does not contain any objects. 我的问题是,当我收到nservicebus消息并从单例类获取列表时,有时该列表不包含任何对象。 And there are times that the objects exist. 有时,对象存在。 So, what I did is every time I get the singleton instance I execute 'GetHashCode' and confirmed that there are 2 different instances of the Singleton class. 因此,我所做的就是每次获取单例实例时都执行“ GetHashCode”并确认有2个不同的Singleton类实例。 What did I implement incorrectly with my code? 我用代码错误地实现了什么?

public class SingletonClass
{
    private static readonly object _lockObj = new object();
    private static readonly object _lockObjList = new object();

    static SingletonClass _singletonClass;

    private static List<object> _objList;


    private SingletonClass()
    {

    }

    public static SingletonClass Instance
    {
        get
        {
            lock(_lockObj)
            {
                if (null == _singletonClass)
                {
                    _singletonClass= new SingletonClass();
                    _objList = new List<object>();
                }
                return _singletonClass;
            }
        }
    }

    public List<obj> GetList()
    {
        lock(_lockObjList)
        {
            return _objList;
        }
    }

    public void UpdateProgress(int index, double value)
    {
        lock(_lockObjList)
        {
            _objList[index].Progress = value;
        }
    }


    public void SetList(List<obj> objs)
    {
        lock(_lockObjList)
        {
            _objList = objs;
        }
    }
}
public class MessageHandler : HubInvoker<MessageHub>
{

    public MessageHandler () {}

    public void OnReceiveMessage(object sender, MessageArgs args)
    {
        var list = SingletonClass.Instance.GetList();
        if(list != null){
            var i = 0;
            for(; i < list.Length && list[i].Id == args.Id; i++);

            if(i < list.Length)
            {
                SingletonClass.Instance.UpdateProgress(i, args.Progress);
            }
        }
    }
}
public class ObjController
{
     public ObjController() {}

     public void SetList(List<obj> objs)
     {
         SingletonClass.Instance.SetList(objs);
     }
}

EDITED EDITED

I've added some codes above for more information of my implementation. 我在上面添加了一些代码,以获取有关实现的更多信息。

You will need to implement double checked locking, see here and depending on your setup a volatile keyword as well. 您将需要实施双重检查的锁定,请参见此处,并根据您的设置使用volatile关键字。 See below code: 请参阅以下代码:

public static SingletonClass Instance
{
    get
    {
        if (null == _singletonClass)
        {
            lock (_lockObj)
            {
                if (null == _singletonClass)
                {
                    _singletonClass = new SingletonClass();
                    _objList = new List<object>();
                }
                return _singletonClass;
            }
        }
    }
}

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

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