InvalidOperationException When calling ResourceManager.GetString

后端 未结 2 1649
再見小時候
再見小時候 2021-01-12 00:13

My application throw the exception occasionally:

Exception type: InvalidOperationException Exception message: Collection was modified; enumeration

相关标签:
2条回答
  • 2021-01-12 00:21

    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()
    
    0 讨论(0)
  • 2021-01-12 00:30

    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
    }
    
    0 讨论(0)
提交回复
热议问题