[英]InvalidOperationException When calling ResourceManager.GetString
My application throw the exception occasionally: 我的应用偶尔抛出异常:
Exception type: InvalidOperationException Exception message: Collection was modified; 异常类型:InvalidOperationException异常消息:集合已被修改; enumeration operation may not execute. 枚举操作可能无法执行。
And here's stacktrace 这是堆栈跟踪
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)
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! 有人可以帮忙吗?这太奇怪了!
After a long time checking, I found the root cause. 经过长时间的检查,我找到了根本原因。 And here's my code cause above issue: 这是我的代码导致上述问题:
AppDomain.CurrentDomain.GetAssemblies().
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". 因为此方法尝试加载生成的程序集,如“web_adg_gfgt_dfd.dll”,并且可以在IIS回收时删除它们。为了修复它,我们只需要避免加载“生成的程序集”。
Therefore we have 2 way for fixing: 因此我们有两种方法可以解决:
1.Filter "generated assemblies": 1.过滤“生成的组件”:
AppDomain.CurrentDomain.GetAssemblies().Where(i => i.IsDynamic == false).ToList()
2.Using this method : 2.使用这种方法:
BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList()
Actually InvalidOperationException Exception message: Collection was modified; 实际上是InvalidOperationException异常消息:集合已被修改; enumeration operation may not execute means: 枚举操作可能不执行意味着:
We are changing the elements in the collection while looping over it with foreach. 我们正在改变集合中的元素,同时使用foreach循环它。
I think this should solve your problem. 我认为这应该可以解决你的问题。
foreach (var func in list.ToList())
{
//Do your stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.