Reputation: 476
I have a program that loads plugins at runtime. I iterate through each directory in the "Plugins" directory and attempt to load the DLL found. Each plugin has its own directory with the plugin DLL file and another directory called "Dependencies". If the plugin's DLL invokes the AssemblyResolve event I do the following:
Plugins that have no 3rd party dependencies or simple ones like Newtonsoft.Json or NHibernate load just fine. I have one plugin that depends on a DLL we'll just call "custom_library.dll." When the AssemblyResolve event is fired looking for this dependency I can confirm the file is in the Dependencies directory like it should be and even a File.Exists() call returns true.
Unfortunately the Assembly.LoadFrom() call for this DLL throws a FileLoadException with the message "Could not load file or assembly 'custom_library.dll, Version=5.3.136.0, Culture=neutral, PublicKeyToken=null'. Nothing else is provided in the exception beyond HRESULT -2146233079. I can also reference this DLL through Visual Studio without problem.
Why am I getting this exception when trying to load the DLL at runtime instead of through Visual Studio's Add Reference feature?
Upvotes: 1
Views: 3005
Reputation: 476
Assembly.LoadFrom was failing because the 3rd party DLL that I was trying to reference was built as a .NET Framework project and I was in a .NET Core project. I confirmed this by creating my own DLL with one class as a .NET Framework Class Library project and received the same error. Once I took the exact same thing and built it targeting .NET Standard 2.0 everything loaded fine.
I spoke with the vendor and once they provided a different DLL that targeted .NET Standard the DLL was able to load and find all of its dependencies without issue.
Upvotes: 1