My application throw the exception occasionally:
Exception type: InvalidOperationException Exception message: Collection was modified; enumeration
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".
Therefore we have 2 way for fixing:
1.Filter "generated assemblies":
AppDomain.CurrentDomain.GetAssemblies().Where(i => i.IsDynamic == false).ToList()
2.Using this method :
BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList()
Actually InvalidOperationException Exception message: Collection was modified; enumeration operation may not execute means:
We are changing the elements in the collection while looping over it with foreach.
I think this should solve your problem.
foreach (var func in list.ToList())
{
//Do your stuff
}