I am unable to understand the root cause of following error, because when I attach the debugger it will not occur.
Invalid operation exception: collection was modified; enumeration operation may not execute.
This is the WCF server in a Window based service. The method NotifySubscribers() is always called by the service whenever there is a data event. When a Windows Forms client subscribes to it, then the subscriber ID is added to the subscribers dictionary, and when the client un subscribes, the ID is deleted from the dictionary. The error seems to happen when the client unsubscribes. When the next time the NotifySubscribers() method is called, the foreach() loop will fail with the error in the subject line. But when the debugger is attached and the client unsubscribes, the code executes fine.
Do you see a problem in my code? Do I need to make the dictionary the thread-safe?