【问题标题】:InvalidOperationException When calling ResourceManager.GetString调用 ResourceManager.GetString 时出现 InvalidOperationException
【发布时间】:2016-01-26 13:46:39
【问题描述】:

我的应用程序偶尔会抛出异常:

异常类型:InvalidOperationException 异常消息: 收藏已修改;枚举操作可能无法执行。

这里是堆栈跟踪

    Exception type: InvalidOperationException 
    Exception message: Collection was modified; enumeration operation may not execute.
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlMark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(CultureInfo lookForCulture, StackCrawlMark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
   at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
   at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
   at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)

这是我的代码:

public IList<Function> MapWithLanguage(IList<Function> list)
{
    if (list == null)
    {
        return null;
    }
    var currentResource = Type.GetType("Fanex.Athena.Models.ViewModel.Menu, Fanex.Athena.Models");
    ResourceManager rm = new ResourceManager(currentResource);
    var newList = new List<Function>();
    foreach (var func in list)
    {
        newList.Add(new Function
        {
            Name = rm.GetString("Menu_" + func.FunctionId),
        });
    }
    return newList;
}

任何人都可以帮忙吗?太奇怪了!

【问题讨论】:

  • 分享产生此异常的位置。
  • 我刚刚更新了我的代码!请看@Backs

标签: c# asp.net .net asp.net-mvc asp.net-mvc-4


【解决方案1】:

查了很久,找到了根本原因。 这是我的代码导致上述问题:

AppDomain.CurrentDomain.GetAssemblies().

因为这种方法会尝试加载生成的程序集,例如“web_adg_gfgt_dfd.dll”,并且可以在 IIS 回收时将其删除。所以要修复它,我们只需要避免加载“生成的程序集”即可。

因此我们有两种修复方法:

1.过滤“生成的程序集”:

AppDomain.CurrentDomain.GetAssemblies().Where(i => i.IsDynamic == false).ToList()

2.使用此方法:

BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList()

【讨论】:

  • 不错的收获!如果 MS 的某个人可以验证这是否真的与 ResourceManager.GetString 错误的问题有关,以及它是否是 .net 4 运行时中的错误,那就更好了。在针对 .net 2 运行时,我至少没有遇到这个问题。从目标框架 3.5 切换到 4.6 后才开始看到这一点。谢谢!
  • @Alex Nguyen,我在您的问题代码片段中没有看到“AppDomain.CurrentDomain.GetAssemblies()”
  • 我也有同样的异常但是不要调用 AppDomain.CurrentDomain.GetAssemblies()
  • @cosset 如果你不动态加载程序集,你的问题和我的不一样
  • 这个答案非常诱人,但似乎并没有真正给出答案。如果我们调用的是ResourceManager.GetString,我们如何控制AppDomain中的程序集?
【解决方案2】:

实际上是 InvalidOperationException 异常消息:集合已修改;枚举操作可能无法执行意味着:

我们正在更改集合中的元素,同时使用 foreach 对其进行循环。

我认为这应该可以解决您的问题。

foreach (var func in list.ToList())
{
//Do your stuff
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多