集合已被修改;枚举操作可能不会执行。我无法找到这个错误的底部,因为当附加调试器时,它似乎不会发生。下面是密码。这是Windows服务中的WCF服务器。每当有数据事件时,服务都会调用NotifySubscriber方法(随机间隔,但不经常-每天大约800次)。当Windows窗体客户端订阅时,订阅者ID将添加到订阅者字典中,而当客户端取消订阅时,它将从字典中删除。当客户端取消订阅时(或之后)发生错误。似乎下一次调用NotifySubscriber()方法时,foreach()循环会失败,主题行中的错误会导致失败。该方法将错误写入应用程序日志,如下代码所示。当附加调试器和客户端取消订阅时,代码执行良好。你看到这个代码有问题了吗?我需要让字典线程安全吗?[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);
}
}}
4 回答
神不在的星期二
TA贡献1963条经验 获得超6个赞
foreach(Subscriber s in subscribers.Values)
foreach(Subscriber s in subscribers.Values.ToList())
元芳怎么了
TA贡献1798条经验 获得超7个赞
.ToList()
:
public void NotifySubscribers(DataRecord sr) { foreach(Subscriber s in subscribers.Values.ToList()) { ^^^^^^^^^ ...
月关宝盒
TA贡献1772条经验 获得超5个赞
lock (subscribers) { foreach (var subscriber in subscribers) { //do something } }
- 4 回答
- 0 关注
- 881 浏览
添加回答
举报
0/150
提交
取消