ConcurrentDictionary InvalidOperationException c#

时间:2015-10-24 07:45:33

标签: c# asp.net asp.net-mvc list asp.net-web-api

这是错误:

  

System.InvalidOperationException:修改了集合;枚举操作可能无法执行       在System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource资源)
        在System.Collections.Generic.List 1.Enumerator.MoveNextRare()
at System.Collections.Generic.List
1.Enumerator.MoveNext()
           在System.Linq.Enumerable.WhereListIterator 1.MoveNext()
at System.Linq.Enumerable.Count[TSource](IEnumerable
1来源)
  块引用

我使用静态字典来表示web api

这是我用于我的网络API的课程:

public class UsersSecureProvider
{
    public static ConcurrentDictionary<short, List<UserSecure>> _Users = new ConcurrentDictionary<short, List<UserSecure>>();

    public bool Add(short Group, UserSecure Message)
    {
        try
        {
            var GetList = GetByKey(Group);

            if (GetList != null)
            {
                GetList.Add(Message);
                return Update(Group, GetList, GetList);
            }
            else
            {
                GetList = new List<UserSecure>();
                GetList.Add(Message);
                return Add(Group, GetList);
            }
        }
        catch { }

        return false;
    }

    private bool Add(short key, List<UserSecure> SendUser)
    {
        return _Users.TryAdd(key, SendUser);
    }

    public bool Remove(short Key)
    {
        List<UserSecure> listremove;
        return _Users.TryRemove(Key, out listremove);
    }

    public List<UserSecure> GetByKey(short Group)
    {
        var listView = new List<UserSecure>();
        if (_Users != null)
        {
            var getList = _Users.TryGetValue(Group, out listView);
        }

        return listView;
    }

    public bool Update(short Group, List<UserSecure> oldlist, List<UserSecure> newlist)
    {
        return _Users.TryUpdate(Group, newlist, oldlist);
    }

    public void Clear()
    {
        _Users.Clear();
    }

    public ConcurrentDictionary<short, List<UserSecure>> GetAll()
    {
        return _Users;
    }

    public bool UpdateListByUser(short Group, List<UserSecure> newlist)
    {
        var OldList = GetByKey(Group);
        return _Users.TryUpdate(Group, newlist, OldList);
    }
}

我打电话给班级

 var _providers = new UsersSecureProvider();

        List<UserSecure> GetAll = _providers.GetByKey(1);

        if (GetAll != null && GetAll.Any() && GetAll.Where(w => w.UserID == UserID && w.Key == UniqueSecure).Count() > 0)
        {
            result = true;
        }
        else
        {
                _providers.Add(1, new UserSecure { UserID = UserID, Key = UniqueSecure });
        }

为什么我会收到此错误异常?

谢谢。

2 个答案:

答案 0 :(得分:2)

此:

List<UserSecure> GetAll = _providers.GetByKey(1);

返回对基础集合的引用。对可能通过您拥有的其他WebAPI操作之一进行修改的列表的相同引用。您不能同时枚举和修改List<T>

而是创建一个新的List<T>并枚举它:

List<UserSecure> GetAll = _providers.GetByKey(1).ToList();

答案 1 :(得分:-1)

如果您的应用程序是多线程,则重新编写它以使用信号量。例如

private static object _sync = new object();
public List<UserSecure> GetByKey(short Group)
{
    lock(_sync)
    {
        var listView = new List<UserSecure>();
        if (_Users != null)
        {
            var getList = _Users.TryGetValue(Group, out listView);
        }

        return listView;
    }
}