• Register
0 votes
60 views

Problem :

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?

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I think that the SignalData is indirectly changing the subscribers dictionary under the hood during the loop and leading this is leading to that message. You can cross check it by changing

foreach(Subscriber s in subscribers.Values)

To

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

If I am not wrong after doing above changes your problem will disappear.

9 7 4
38,600 points

Related questions

1 vote
1 answer 2 views
2 views
Problem: Need to get rid of this. Collection was modified; enumeration operation might not execute.
asked Mar 25 PkGuy 13.1k points
0 votes
1 answer 44 views
44 views
Problem : I am new to C#. I want to know how can I get the value of a given key in a following exception which I am currently facing in C# in a way it affects all the generic classes? Below is the exception which I am currently facing. "The given key was not present in the dictionary." How to fix above exception.
asked Feb 24, 2020 mphil 2.3k points
0 votes
1 answer 3 views
3 views
Problem: when I try to run it on Windows 7 it gives me the following error: System.ServiceModel.CommunicationException: An error occurred while making the HTTP request to https://nfce-homologacao.svrs.rs.gov.br/ws/NfeStatusServico/NfeStatusServico2.asmx.
asked 4 days ago ummesalma 25.2k points
0 votes
1 answer 2 views
2 views
Problem: Task going wrong here > does not contain a definition for getawaiter
asked Apr 8 Azlan2020 2.3k points
0 votes
1 answer 8 views
8 views
Problem: The service call returns the message: System.ServiceModel.ServiceActivationException: The requested service, 'http://localhost:83/ABCStaticData/StaticDataService.svc' could not be activated. See the server's diagnostic trace logs for more information.
asked Mar 20 ummesalma 25.2k points
0 votes
1 answer 10 views
10 views
Problem: After debugging and installing windows service in windows 8 I have an error when I try to start a windows service:"The Windows could not start service on local computer Error 5 Access is denied"
asked Mar 8 ummesalma 25.2k points
0 votes
1 answer 68 views
68 views
Problem : I am having my radcombo machines on aspx page and whenever I try to select any option from them then it gives me following `Server Error in '/' Application. Exception Details: System.InvalidOperationException: Operation is not valid due to the current state of the object. My Version Information: Microsoft .NET Framework Version: 2.0.50727.3634 ASP.NET Version: 2.0.50727.3634
asked Dec 2, 2019 alecxe 7.5k points
0 votes
1 answer 18 views
18 views
Problem: an error occurred when verifying security for the message.
asked Feb 22 Dan phillip 4.8k points
0 votes
1 answer 21 views
21 views
Is there a standard algorithm or best practice on how to implement GetHashCode for my custom classes?
asked Aug 31, 2020 Sofi55 1.1k points
0 votes
1 answer 18 views