ASP.NET 4.5 application warmup causes error determining loaded assemblies

140 views Asked by At

I have a library module that we use in all of our web applications that lists out all the loaded assemblies (and their version number) into our log4net log file in the Application_Start method of an ASP.NET webforms application.

This has worked flawlessly for ages - until today, when I tried to use it after an "application warmup" method (described by Scott Guthrie in his blog post had run after deployment to IIS (and before the ASP.NET's Application_Start ever ran) - now suddenly I'm getting an error:

System.NotSupportedException: The invoked member is not supported in a dynamic assembly.

The code that reports the loaded assemblies looks like this:

public static void ReportLoadedAssemblies(this ILog log) 
{
    if (log.IsInfoEnabled) 
    {
        log.Info("Loaded assemblies:");

        IEnumerable<Assembly> appAssemblies = AppDomain.CurrentDomain.GetAssemblies().OrderBy(c => c.ManifestModule.Name);

        foreach (Assembly asm in appAssemblies) 
        {
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);

            if (!fvi.CompanyName.Contains("Microsoft")) 
            {
                log.Info(String.Format("{0, -45} : {1, -15} {2}", fvi.FileDescription, fvi.FileVersion, asm.Location));
            }
        }
    }
}

I'm not 100% clear on which line exactly throws the error - I suspect it's this one:

IEnumerable<Assembly> appAssemblies = AppDomain.CurrentDomain.GetAssemblies().OrderBy(c => c.ManifestModule.Name);

So what exactly is this message telling me? And how can I still get my loaded assemblies in this situation?

1

There are 1 answers

1
Adriano Repetti On BEST ANSWER

I think error is not on

IEnumerable<Assembly> appAssemblies = AppDomain.CurrentDomain.GetAssemblies().OrderBy(c => c.ManifestModule.Name);

Because accessing Assembly.Location property when assembly is dynamically generated will throw NotSupportedException.

So what exactly is this message telling me?

That a dynamically generated assembly has not any location then you can't access that property. Yes it may return an empty string but then it would be the same as assemblies created with Assembly.Load()...to be serious I can't understand rationale of this choice but that's it.

And how can I still get my loaded assemblies in this situation?

Simply check for it:

foreach (Assembly asm in appAssemblies.Where(x => !x.IsDynamic)) 
{
    // Your code
}   

Or easier:

var appAssemblies = AppDomain.CurrentDomain.GetAssemblies()
    .Where(x => !x.IsDynamic)
    .OrderBy(c => c.ManifestModule.Name);