I have encountered a problem when loading sub-assemblies of loosely coupled .NET core projects.
A short summary of the structure: I have an executable project that gets some interfaces on a .NET Core Library in the same solution. Then I have a .NET Core Library projects that contain a class that implements a specific interface (IDependencyProvider) from the interface project. These other projects are not directly referenced by the executable but are loaded as *.dll at runtime.
Here is some code of the loading process:
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var preloadedAssemblies = Directory.GetFiles(path, "*.dll").Select(Assembly.LoadFile).ToList();
var type = typeof(IDependencyProvider);
var types = preloadedAssemblies
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));
As long as the unreferenced project has no dependencies, the whole thing works very well. Although dependencies of the unreferenced projects are copied as DLL, they cannot be loaded for some unexplainable reason.
Here is the error message:
System.IO.FileNotFoundException: 'Could not load file or assembly '...'. The system cannot find the file specified.'
Here is a repository that shows the problem. The bug can be reproduced on the bugged branch: https://github.com/jonashammerschmidt/NotReferencedAssemblyTest
What might be the cause of this problem?