nrod
nrod

Reputation: 398

Collection was modified error in asp.net MVC 4

I am trying to find the source of the following error in my ASP.NET MVC 4 application.

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

Stack trace: at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Web.Mvc.MvcHandler.RemoveOptionalRoutingParameters()
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

The problem is that all (the few) places I have a Collection being iterated I am not changing the Collection structure in any way. The stack trace does not help that much as it does not contain any reference to my code.

I have read lots of posts on the subject and basically that's what they mention as a solution. Any one knows other solution for such a problem? Any hints on how to get some more information on the error?

Upvotes: 1

Views: 1780

Answers (1)

Bart
Bart

Reputation: 5203

nrod, for completeness it would be good to post your code.

However from your stack trace I can already see the error is occuring in the (MVC) framework (e.g. System.Web). Íf the stack trace points to your own code the problem is normally due to manipulating a collection (.e.g removing, adding) while iterating over it (for instance in a foreach loop). Some suggestions are to to iterate in reversed way or to use a .ToList() on your collection, see: Collection was modified; enumeration operation may not execute Collection was modified; enumeration operation may not execute - why?

However your stack trace indicates the problem is from calls from your code to the (MVC) framework. This kind of problem can be caused by using instance member variables in a shared custom implemented IRouteHandler.GetHttpHandler(). That is because instance member are NOT thread safe, as documented on MSDN: http://msdn.microsoft.com/en-us/library/system.web.http.webhost.httpcontrollerroutehandler%28v=vs.108%29.aspx

Your stack trace indicates you are indeed using member vars. The error could then occur when doing lots of concurrent calls (e.g. in a production environment).

You can fix the problem by refactoring the code not to use any instance members. This might lead to somewhat monolithic and repetitive code. For instance when refactoring to using only local variables. But you can also try and refactor use to public static members or static functions instead, since these ARE thread safe.

After fixing this problem myself I decided it would be good to always do some load testing of new code, as otherwise these kinds of problems will only be detected on production!

Upvotes: 2

Related Questions