Asp.net mvc mvc3 EditorFor/DisplayFor Trows集合已修改;枚举操作不能执行

Asp.net mvc mvc3 EditorFor/DisplayFor Trows集合已修改;枚举操作不能执行,asp.net-mvc,Asp.net Mvc,我们有一个MVC3网站在开发框中运行良好,但在投入生产后,它经常抛出异常“Collection was modified;enumeration operation may not execute.”。例外情况来自多个页面。到目前为止,我发现的唯一常见模式是:所有页面都调用部分视图,部分视图使用EditorFor或DisplayFor,这就是异常抛出的地方。下面是一个堆栈跟踪: System.InvalidOperationException: Collection was modified;

我们有一个MVC3网站在开发框中运行良好,但在投入生产后,它经常抛出异常“Collection was modified;enumeration operation may not execute.”。例外情况来自多个页面。到目前为止,我发现的唯一常见模式是:所有页面都调用部分视图,部分视图使用EditorFor或DisplayFor,这就是异常抛出的地方。下面是一个堆栈跟踪:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
   at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths)
   at System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions)
   at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate)
   at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData)
   at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData)
   at System.Web.Mvc.Html.DisplayExtensions.DisplayFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression)
   at ASP._Page_Views_Shared__ContactDetails_cshtml.Execute() in d:\websites\kpa.hrm.web\Views\Shared\_ContactDetails.cshtml:line 11
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
System.invalidoOperationException:集合已修改;枚举操作不能执行。
位于System.Collections.Generic.List`1.Enumerator.MoveNextRare()处
在System.Linq.Enumerable.d_u71`1.MoveNext()中
在System.Web.Mvc.ViewEngineCollection.Find(Func`2查找,布尔跟踪搜索路径)
位于System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper Html、ViewDataDictionary viewData、String templateName、DataBoundControlMode、getViewNames委托getViewNames、getDefaultActions删除getDefaultActions)
位于System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper Html、ModelMetadata元数据、字符串htmlFieldName、字符串templateName、DataBoundControlMode模式、对象附加视图数据、ExecuteTemplateDelegate executeTemplate)
位于System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper Html、ModelMetadata元数据、字符串htmlFieldName、字符串templateName、DataBoundControlMode模式、对象附加视图数据)
在System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 Html,Expression`1 Expression,String templateName,String htmlFieldName,DataBoundControlMode模式,Object additionalViewData)
在System.Web.Mvc.Html.DisplayExtensions.DisplayFor[TModel,TValue](HtmlHelper`1 Html,Expression`1 Expression)
在d:\websites\kpa.hrm.web\Views\Shared\\u ContactDetails.cshtml第11行的ASP.\u Page\u Views\u Shared\u ContactDetails\u cshtml.Execute()中
在System.Web.WebPages.WebPageBase.ExecutePageHierarchy()中
在System.Web.Mvc.WebViewPage.ExecutePageHierarchy()中
位于System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext、TextWriter writer、WebPageRenderingBase起始页)
在System.Web.Mvc.ViewResultBase.ExecuteSult(ControllerContext上下文)中
在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass1C.b_uuu19()中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器、ResultExecutingContext预文本、Func`1 continuation)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器、ResultExecutingContext预文本、Func`1 continuation)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器、ResultExecutingContext预文本、Func`1 continuation)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器、ResultExecutingContext预文本、Func`1 continuation)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器、ResultExecutingContext预文本、Func`1 continuation)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext ControllerContext,IList`1 filters,ActionResult ActionResult)
位于System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)

如果您正在迭代任何静态或单例集合,请确保它们是线程安全的。我没有找到使用静态或单例集合的任何位置,视图只是传递给视图模型。然后,您很可能正在迭代中从集合中添加或删除对象。请记住,不能在foreach中修改集合。