Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

c# - InvalidOperationException When calling ResourceManager.GetString

My application throw the exception occasionally:

Exception type: InvalidOperationException Exception message: Collection was modified; 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...