ConcurrentDictionary InvalidOperationException c#

本文关键字:InvalidOperationException ConcurrentDictionary | 更新日期: 2023-09-27 18:18:59

错误:

系统。InvalidOperationException: Collection被修改;不能执行枚举操作。
在System.ThrowHelper。ThrowInvalidOperationException (ExceptionResource资源)
at System.Collections.Generic.List 1.Enumerator.MoveNextRare()
at System.Collections.Generic.List
. enumerator . movenext ()
在System.Linq.Enumerable.WhereListIterator 1.MoveNext()
at System.Linq.Enumerable.Count[TSource](IEnumerable
1源)
引用

我使用静态字典web api

这是我的类,我使用我的web 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 });
        }

为什么我收到这个错误异常?

谢谢。

ConcurrentDictionary InvalidOperationException c#

This:

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

返回对基础集合的引用。对列表的相同引用可能通过你的其他WebAPI操作之一被修改。List<T>不能枚举,也不能修改。

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

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

如果您的应用程序是多线程的,建议使用信号量。例如

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;
    }
}
相关文章:
  • 没有找到相关文章