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

馋奶兔 提交于 2019-12-12 12:19:53

问题


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


回答1:


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




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




回答3:


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)



来源:https://stackoverflow.com/questions/40584562/method-does-not-have-an-implementation-when-loading-assemblies-into-a-new-appdom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!