InvalidOperationException When calling ResourceManager.GetString

My application sometimes changes an exception:

Exception Type: InvalidOperationException Exception Message: Collection has been modified; enumeration operation cannot be performed.

And here is the 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 is 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; } 

Can anybody help? This is so strange!

+5
source share
2 answers

After a long check, I found the root cause. And here my code causes the problem above:

 AppDomain.CurrentDomain.GetAssemblies(). 

Since this method tries to load the generated assemblies, such as "web_adg_gfgt_dfd.dll", and they can be removed during IIS processing. Therefore, to fix this, we only need to avoid loading the “generated assemblies”.

Therefore, we have 2 ways to fix:

1.Filter "generated assemblies":

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

2. Using this method:

 BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList() 
+2
source

Actually InvalidOperationException Exception message: collection has been modified; an enumeration operation may not be performed:

We change the elements in the collection by looping through it with foreach.

I think this should solve your problem.

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

Source: https://habr.com/ru/post/1232000/


All Articles