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

3

5

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

Psephology answered 14/11, 2016 at 8:29 Comment(2)
"method BeforeLoad is virtual and an implementation of an interface method". is it really, though? Check all the answers here to make sure you're not hitting a versioning/loading conflict of some sort. ILSpy/ILDAsm aren't going to report any problems since the assembly is structurally valid.Downcomer
Thanks, @JeroenMostert. I'll have a look at it.Flunk
P
7

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 ...

Psephology answered 16/11, 2016 at 10:25 Comment(0)
B
2

While debugging, I added an exception and when the exception was thrown , I opened the Modules window (Debug Menu --> Windows --> Modules, or [Ctrl + D, M]), and then I realised my code was using a DLL from a different place that I was expecting, I replaced that old DLL with the new one and then it worked.

Bougie answered 18/5, 2018 at 1:40 Comment(0)
P
0

I just encountered a similar problem, I had made an interface of the UserManager class in Microsoft.AspNet.Identity.Core (for dependency injection via Unity). In a unit test I verified the Unity registrations and encountered this exception even though my application compiled correctly.

Turns out the project I was testing and the Unit Test project had a different version of Microsoft.AspNet.Identity.Core installed (visible via NuGet Package Manager)

Peddle answered 28/9, 2018 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.