加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – InvalidOperationException调用ResourceManager.GetString

发布时间:2020-12-15 07:42:19 所属栏目:百科 来源:网络整理
导读:我的应用偶尔抛出异常: Exception type: InvalidOperationException Exception message: Collection was modified; enumeration operation may not execute. 这是堆栈跟踪 Exception type: InvalidOperationException Exception message: Collection was mo
我的应用偶尔抛出异常:

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

这是堆栈跟踪

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,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,StackCrawlMark& stackMark)
   at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture,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;
}

有人可以帮忙吗?太奇怪了!

解决方法

经过长时间的检查,我找到了根本原因.
这是我的代码导致上述问题:
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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读