InvalidOperationException 调用 ResourceManager.GetString 时 [英] InvalidOperationException When calling ResourceManager.GetString
问题描述
我的应用程序偶尔会抛出异常:
My application throw the exception occasionally:
异常类型:InvalidOperationException 异常消息:收藏被修改;枚举操作可能不会执行.
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, 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;
}
有人可以帮忙吗?这太奇怪了!
Anybody can help?it's so weird!
推荐答案
经过长时间的检查,我找到了根本原因.这是我的代码导致上述问题:
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".
因此我们有两种修复方法:
Therefore we have 2 way for fixing:
1.过滤生成的程序集":
1.Filter "generated assemblies":
AppDomain.CurrentDomain.GetAssemblies().Where(i => i.IsDynamic == false).ToList()
2.使用此方法:
BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList()
这篇关于InvalidOperationException 调用 ResourceManager.GetString 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!