Share via


Exception message: Collection was modified; enumeration operation may not execute.

Question

Friday, August 16, 2013 3:26 PM

Hi all,

I am seeing this record on Application Event Log on live environment of that web application. its an ASP.NET web application and it seems that the error is related to URLRouting module, stack trace is below.

Any one has an idea to fix that issue?

Thanks for any help,

Adem.

Exception type: InvalidOperationException 
    Exception message: Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext)
   at System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

All replies (4)

Saturday, August 17, 2013 11:11 AM ✅Answered

Hi Adem,

The answer from Wasabi Fan demonstrates a common beginner's mistake that will cause this Exception. That particulcar mistake can usually be fixed by iterating over a copy of the collection (as suggested by Muthuraja) or by using a "for" loop instead of "foreach".

However, that is not the only way this exception can occur. It will also occur in situations where a collection is shared among multiple threads and one thread is modifying the collection while another thread is iterating over it, as in the following example:

class Program
{
    static List<int> myList = new List<int>();

    static void Main(string[] args)
    {
        myList.AddRange(new[] { 1, 2, 3, 4, 5 });

        var ts = new ThreadStart(() => {
            Thread.Sleep(100);
            myList.RemoveAt(0);
        });

        var otherThread = new Thread(ts);
        otherThread.Start();

        foreach (var item in myList)
        {
            Thread.Sleep(50);
            Console.WriteLine(item);
        }

        Console.WriteLine("Press any key to quit...");
        Console.ReadKey(true);
    }
}

From your stack trace it seems like the exception is occuring somewhere in the ASP.NET framework, so my guess it that the framwork is trying to iterate over a collection of routing information in one thread and fails because some other thread has modified the collection.

Does your application modifiy the routing information automatically at runtime? What does that code look like? Is it possible to replace the collection with a new collection instead of modifying the existing one? Or does the framwork provide some locking mechanism that you can use to make sure no one else is accessing the collection while it is being modified?

Unfortunately, I don't know how the UrlRoutingModule can/should be used. Perhaps you should ask about this in an ASP.NET forum.

Marcus Björklund


Saturday, August 17, 2013 5:37 AM

That exception most likely means that somewhere in your code you are using a foreach loop or otherwise enumerating a collection while modifying it.

For example:

List<int> List = new List<int>() {0,1,2,3,4,5};
foreach (int i in List)
{
    List.Add(i);
}

This (particularly useless) piece of code would throw the same exception, because it modifies the collection while enumerating through it.

Hope this answers your question!

Wasabi Fan


Saturday, August 17, 2013 10:50 AM

Hi,

As Wasabi Fan mentioned, it could a problem when changing the collection value while accessing it. This is applies to your scenario then you can avoid that. 

List<int> myList= new List<int>() {0,1,2,3,4,5};
foreach (int i in myList.Values)
{
// Process List 

}

Best Regards

Muthuraja


Monday, August 19, 2013 2:24 AM

Hello,

Welcome to MSDN forum.

From your description, this thread is related to ASP.NET,ASP.NET forum is more suitable for it ,I would like to suggest to ASP.NET forum.

ASP.NET forum :http://forums.asp.net/

This moederator and community will be gald to help you.

Lilia Gong
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.