簡體   English   中英

“集合被修改”錯誤嘗試枚舉Bundle中的文件

[英]“Collection was modified” error attempting to Enumerate Files in a Bundle

我在MVC頁面上遇到一個棘手的錯誤,只有在網站上有多個用戶時才會出現這個錯誤。

我得到的錯誤是

收集被修改; 枚舉操作可能無法執行。

嘗試呈現javascript文件Bundle的行上發生錯誤。

 @Scripts.Render("~/Scripts/js")

/ app_start - > BundleConfig.cs - > RegisterBundles中的相關代碼非常標准:

bundles.Add(new ScriptBundle("~/Scripts/js").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/jquery-ui-{version}.js",
            "~/Scripts/jquery.validate*",
            "~/Scripts/jQuery.Buzz/jquery.buzz.js"
        ));

僅當站點上有多個用戶時,才會出現錯誤。 我設法通過在多個瀏覽器中創建多個選項卡並在盡可能快地點擊它們來本地重現問題。

我已經搜索過,但卻找不到其他有類似錯誤的人。 該錯誤意味着在循環時正在修改集合。 我無法看到要添加到包中的文件集合如何在枚舉集合時發生變化。

堆棧跟蹤如下:

[InvalidOperationException: Collection was modified; enumeration operation may not execute.]
   System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +56
   System.Collections.Generic.Enumerator.MoveNextRare() +14277518
   System.Web.Optimization.Bundle.EnumerateFiles(BundleContext context) +266
   System.Web.Optimization.Bundle.GenerateBundleResponse(BundleContext context) +38
   System.Web.Optimization.Bundle.GetBundleResponse(BundleContext context) +61
   System.Web.Optimization.BundleResolver.GetBundleContents(String virtualPath) +214
   System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable`1 assets) +410
   System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat, String[] paths) +35
   ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\..\Views\Shared\_Layout.cshtml:13

發生錯誤時服務器沒有特別重載。 我在服務器上有監控軟件,顯示CPU和內存處於正常水平,同時產生錯誤。

可能導致這種情況的任何想法?

更新:經過一些進一步的搜索后,我發現Bundle正在Controller中以編程方式進行更新。

  BundleTable.Bundles.GetBundleFor("~/Scripts/js").Include("~/Scripts/jquery.mobile-{version}.js");

事實證明這不需要以編程方式添加,所以我將它包含在BundleConfig文件中,該網站似乎現在正在運行。

嘗試將此添加到app_start下的bundle配置文件中

public static void RegisterBundles(BundleCollection bundles) {
        bundles.Add(new ScriptBundle("~/bundles/js1").Include("~/js/jquery-1.8.3.min.js","~/js/jquery-ui.js"));
    }

“〜/ bundles / js1”是捆綁包的名稱。

在您的視圖中添加此行以將腳本呈現到視圖中。

@System.Web.Optimization.Scripts.Render("~/bundles/js1")

應該工作正常...只是不要忘記更改包含的js文件名和包的名稱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM