nvalidOperationException当调用Resou

nvalidOperationException当调用Resou

本文介绍了出现InvalidOperationException当调用ResourceManager.GetString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application throw the exception occasionally:

异常类型: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)

这是我的code:

And here's my code:

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;
}

任何人都可以帮忙吗?它是如此不可思议!

Anybody can help?it's so weird!

推荐答案

很长一段时间检查后,我发现的根本原因。
这是我的code会导致上述问题:

After a long time checking, I found the root cause.And here's my code cause above issue:

AppDomain.CurrentDomain.GetAssemblies().

由于这种方法尝试加载生成的程序集,如web_adg_gfgt_dfd.dll,他们可以被删除当IIS回收。所以要解决这个问题,我们只需要避免加载生成的组件。

Because this method try to load generated assemblies such as "web_adg_gfgt_dfd.dll" and they can be removed when IIS recycle .So to fix it we only need to avoid loading "generated assemblies".

因此​​,我们有2路固定:

Therefore we have 2 way for fixing:

1.Filter生成组件

1.Filter "generated assemblies":

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

2,采用此方法:

2.Using this method :

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

这篇关于出现InvalidOperationException当调用ResourceManager.GetString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:11