Search code examples
c#.net.net-assemblyappdomainassembly.reflectiononly

Method does not have an implementation when loading assemblies into a new AppDomain in ReflectionOnly mode


In our application (solution with 65 projects), all referenced assemblies are analysed in run-time for the presence of Ninject modules (there is some filtering applied too). The modules are loaded later into the Ninject kernel and each module declares bindings for the kernel.

We have adopted a loader that loads the referenced assemblies into a separate assembly in reflection only mode. The difference from the way Ninject can load assemblies from the directory is that the directory can contain assemblies with modules that should not be loaded. And also at the very start, not all referenced assemblies are loaded.

The problem is that the loader (credit to Sacha Barber) cannot load some assemblies with the

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information

and the LoaderExceptions with one entry:

Method 'BeforeLoad' in type 'Lekis.AppBase.Core.BLLBaseCore' from assembly 'AppBaseCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.

Here are some "fun" facts:

  • method BeforeLoad is virtual and an implementation of an interface method
  • last week the loader exception was saying different method did not have an implementation (that method was not virtual) and later, when I have explicitly implemented it, the message said the method was not found.
  • last week the target framework for assembly AppBaseCore was .NET 3.5 and 3 assemblies failed to load
  • now the target framework for assembly AppBaseCore is .NET 4 and 5 assemblies failed to load
  • everything is fine with the application otherwise

There is nothing wrong (obviously) with the assemblies when I checked them with ILSpy and ILDAsm.

At this point, I am really lost and don't know how to approach this issue.

Any help is appreciated.

Thanks


Solution

  • Answering my own question:

    When the exception was thrown, I went up the stack trace and listed the assemblies loaded in the child AppDomain created:

    AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies()
    {System.Reflection.RuntimeAssembly[15]}
    ...
    [13]: {System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
    [14]: {System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
    

    and noticed the two versions of System.Data assembly. The method in question has a parameter of type System.Data.IDbTransaction.

    First one was referenced in a project targeting .NET framework 3.5. After changing it to 4.0 everything works fine.

    What a stupid problem ...