Collection was modified; enumeration operation may

2019-04-11 21:01发布

问题:

we've been getting this error lately and have no idea why, we are not modifying any collection, we even removed most foreach on the pages that we were getting the error. Here is the stacktrace of the error:

   Exception type: InvalidOperationException 
    Exception message: Collection was modified; enumeration operation may not execute.
   en System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   en System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   en System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
   en System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   en System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
   en System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   en System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
   en System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   en System.Linq.Enumerable.<ReverseIterator>d__a0`1.MoveNext()
   en System.Web.Mvc.FilterProviderCollection.<RemoveDuplicates>d__b.MoveNext()
   en System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   en System.Linq.Enumerable.<ReverseIterator>d__a0`1.MoveNext()
   en System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   en System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   en System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   en System.Web.Mvc.FilterInfo..ctor(IEnumerable`1 filters)
   en System.Web.Mvc.ControllerActionInvoker.GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
   en System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   en System.Web.Mvc.Controller.ExecuteCore()
   en System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   en System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
   en System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   en System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
   en System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   en System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Any help is appreciated.

回答1:

More code of your list instantiation would help.

Realistically, I would check that the enumerable that you pass to the list constructor is not changed. When you use IEnumerable, you are generating the enumerable at that point, whereas IQueryable allows you to defer the generation of the enumerable until a control point of execution.

To me, it looks like you are generating an IEnumerable To pass to the list constructor, but you are modifying the source list before you do.



回答2:

I've been seeing this issue pop up for us as well, so I did some digging. It's pretty clear this happens deep within MVC based on the stack trace, but it looks like MVC 5 should fix this. The version on the left is MVC4 and the one on the right is the current version of MVC5 at the time of posting this. If you check out the RemoveDuplicates() method, it looks like it's been modified to prevent exceptions like we're seeing.