我无法找到这个错误的根源,因为当附加调试器时,它似乎没有发生。

修改集合;枚举操作可能无法执行

下面是代码。

这是Windows服务中的WCF服务器。只要有数据事件,服务就会调用NotifySubscribers()方法(随机间隔,但不经常——大约每天800次)。

When a Windows Forms client subscribes, the subscriber ID is added to the subscribers dictionary, and when the client unsubscribes, it is deleted from the dictionary. The error happens when (or after) a client unsubscribes. It appears that the next time the NotifySubscribers() method is called, the foreach() loop fails with the error in the subject line. The method writes the error into the application log as shown in the code below. When a debugger is attached and a client unsubscribes, the code executes fine.

您认为这段代码有问题吗?我需要使字典线程安全吗?

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class SubscriptionServer : ISubscriptionServer
{
    private static IDictionary<Guid, Subscriber> subscribers;

    public SubscriptionServer()
    {            
        subscribers = new Dictionary<Guid, Subscriber>();
    }

    public void NotifySubscribers(DataRecord sr)
    {
        foreach(Subscriber s in subscribers.Values)
        {
            try
            {
                s.Callback.SignalData(sr);
            }
            catch (Exception e)
            {
                DCS.WriteToApplicationLog(e.Message, 
                  System.Diagnostics.EventLogEntryType.Error);

                UnsubscribeEvent(s.ClientId);
            }
        }
    }
    
    public Guid SubscribeEvent(string clientDescription)
    {
        Subscriber subscriber = new Subscriber();
        subscriber.Callback = OperationContext.Current.
                GetCallbackChannel<IDCSCallback>();

        subscribers.Add(subscriber.ClientId, subscriber);
        
        return subscriber.ClientId;
    }

    public void UnsubscribeEvent(Guid clientId)
    {
        try
        {
            subscribers.Remove(clientId);
        }
        catch(Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Unsubscribe Error " + 
                    e.Message);
        }
    }
}

当前回答

我也有同样的问题,当我使用for循环而不是foreach时,它得到了解决。

// foreach (var item in itemsToBeLast)
for (int i = 0; i < itemsToBeLast.Count; i++)
{
    var matchingItem = itemsToBeLast.FirstOrDefault(item => item.Detach);

   if (matchingItem != null)
   {
      itemsToBeLast.Remove(matchingItem);
      continue;
   }
   allItems.Add(itemsToBeLast[i]);// (attachDetachItem);
}

其他回答

我想指出另一个没有在答案中反映出来的例子。我有一个字典<Tkey,TValue>共享在一个多线程应用程序,它使用ReaderWriterLockSlim来保护读写操作。这是一个引发异常的读取方法:

public IEnumerable<Data> GetInfo()
{
    List<Data> info = null;
    _cacheLock.EnterReadLock();
    try
    {
        info = _cache.Values.SelectMany(ce => ce.Data); // Ad .Tolist() to avoid exc.
    }
    finally
    {
        _cacheLock.ExitReadLock();
    }
    return info;
}

一般来说,它工作得很好,但有时会出现异常。这个问题是LINQ的一个微妙之处:这段代码返回一个IEnumerable<Info>,它在离开锁保护的部分后仍然没有被枚举。因此,它可以在被枚举之前由其他线程更改,从而导致异常。解决方案是强制枚举,例如注释中显示的. tolist()。这样,可枚举对象在离开受保护的部分之前就已经被枚举了。

因此,如果在多线程应用程序中使用LINQ,请注意始终在离开受保护区域之前实现查询。

可能发生的情况是,SignalData在循环过程中间接地更改了订阅者字典,并导致了该消息。您可以通过更改来验证这一点

foreach(Subscriber s in subscribers.Values)

To

foreach(Subscriber s in subscribers.Values.ToList())

如果我是对的,问题就会消失。

调用subscriber . values . tolist()复制订阅者的值。值赋给foreach开始时的单独列表。其他任何东西都不能访问这个列表(它甚至没有变量名!),所以在循环中没有东西可以修改它。

订阅者取消订阅时,您正在枚举期间更改订阅者集合的内容。

有几种方法可以解决这个问题,其中一种是改变for循环,使用显式的.ToList():

public void NotifySubscribers(DataRecord sr)  
{
    foreach(Subscriber s in subscribers.Values.ToList())
    {
                                              ^^^^^^^^^  
        ...

在我看来,一个更有效的方法是另一个列表,你声明你把任何“要删除”的东西都放进去。然后,在完成主循环之后(不使用. tolist()),对“要删除的”列表执行另一个循环,删除每个条目。所以在你的课上你要加上:

private List<Guid> toBeRemoved = new List<Guid>();

然后你把它改成:

public void NotifySubscribers(DataRecord sr)
{
    toBeRemoved.Clear();

    ...your unchanged code skipped...

   foreach ( Guid clientId in toBeRemoved )
   {
        try
        {
            subscribers.Remove(clientId);
        }
        catch(Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Unsubscribe Error " + 
                e.Message);
        }
   }
}

...your unchanged code skipped...

public void UnsubscribeEvent(Guid clientId)
{
    toBeRemoved.Add( clientId );
}

这不仅可以解决您的问题,还可以防止您从字典中创建一个列表,如果其中有很多订阅者,那么创建列表的代价非常昂贵。假设在任何给定迭代中要删除的订阅者列表低于列表中的总数,这样应该更快。当然,如果您在特定的使用情况中有任何疑问,请随意对其进行分析以确保情况属实。

实际上,在我看来,问题似乎是您正在从列表中删除元素,并期望继续读取列表,就像什么都没有发生一样。

你真正需要做的是从头开始,再回到起点。即使您从列表中删除了元素,也可以继续读取它。