Techiio-author
Started by Jofar ArcharNov 6, 2021

Open
Collection was modified; enumeration operation may not execute

0 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWES SHARE

This is a WCF service in a windows carrier. The method notify subscribers() is referred to as via the carrier on every occasion there's a statistics event (at random durations, but no longer very frequently - approximately 800 times in line with day).

Whilst a home windows paperwork purchaser subscribes, the subscriber identity is added to the subscriber's dictionary, and whilst the patron unsubscribes, it's far deleted from the dictionary. The mistake happens when (or after) a client unsubscribes. It seems that the next time the notify subscribers() approach is known as, the foreach() loop fails with the mistake in the concerned line. The approach writes the mistake into the utility log as shown in the code below. When a debugger is attached and a purchaser unsubscribes, the code executes fine.

Do you see a problem with this code? Do I need to make the dictionary thread-safe?

[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);
        }
    }
}

0 Replies

You must be Logged in to reply
Trending Technologies
15
Software40
DevOps47
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Recommended Blogs
1
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance