Assembly is loaded differently via AssemblyResolve?

60 Views Asked by At

I have an app which dynamically loads a DLL from a different folder.

This DLL needs Microsoft.Extensions.Logging.dll, and if I load it before I load my DLL, it works fine:

void f()
{
   Assembly.LoadFrom(@"C:\\DllFolder\\Microsoft.Extensions.Logging.dll");
   Assembly mydll = Assembly.LoadFrom(@"C:\\DllFolder\\MyDLL.dll");
   CallMethodFromMyDll(mydll); // works great :-)
}

Alas, if I use AssemblyResolve to load Microsoft.Extensions.Logging.dll from the same folder, I get a missingExceptionMethod:

void f()
{
   AppDomain.CurrentDomain.AssemblyResolve += MyAssemblyResolveFunction;
   Assembly mydll = Assembly.LoadFrom(@"C:\\DllFolder\\MyDLL.dll");
   CallMethodFromMyDll(mydll); // MissingExceptionMethod.... :-(
}

Assembly MyAssemblyResolveFunction(object sender, ResolveEventArgs args)
{
   var assembly = Assembly.LoadFrom(@"C:\\DllFolder\\Microsoft.Extensions.Logging.dll";

   if (assembly != null)
   {
        // the code gets here!
        return assembly;
   }

   return null;
}

The exception message:

Method not found: 'Microsoft.Extensions.Logging.ILoggerFactory Microsoft.Extensions.Logging.ConsoleLoggerExtensions.AddConsole(Microsoft.Extensions.Logging.ILoggerFactory, Microsoft.Extensions.Logging.LogLevel)'.

I already checked that the assembly loaded by AssemblyResolve is from the same location. Why does it behave differently?

1

There are 1 best solutions below

0
On

Microsoft.Extensions.Logging.ConsoleLoggerExtensions is not in Microsoft.Extensions.Logging.dll but in Microsoft.Extensions.Logging.Console.dll. You need to make sure this dll is also resolved correctly.