2

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?

Fred
  • 3,365
  • 4
  • 36
  • 57
  • 3
    Have you tried `Assembly.LoadFrom()` instead of `LoadFile()`? See [Difference between LoadFile and LoadFrom with .NET Assemblies?](https://stackoverflow.com/q/1477843/3744182) for a discussion of the differences, which include dependency handling. – dbc Jun 21 '20 at 21:48
  • It worked in the provided repository, but I ran into a similar issue when using an ASP.NET Core project and Entity Framework in the unreferenced project. Here is the Exeption: System.IO.FileLoadException: 'Could not load file or assembly 'Microsoft.Extensions.Caching.Abstractions, Version=3.1.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.' – Jonas Hammerschmidt Jun 22 '20 at 08:08
  • The bug can be reproduced on the newest commit in the bugged-branch of the provided repository. – Jonas Hammerschmidt Jun 22 '20 at 08:09
  • Okay, this issue got fixed by https://stackoverflow.com/a/43996389/13263186 – Jonas Hammerschmidt Jun 22 '20 at 08:44
  • So is your question resolved at this point? From your comments, it seems as though [Difference between LoadFile and LoadFrom with .NET Assemblies](https://stackoverflow.com/q/1477843/3744182) and [Could not load file or assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0](https://stackoverflow.com/a/43996389/13263186) together do what you need, agree? – dbc Jun 22 '20 at 14:07
  • 1
    Yes, I agree. Thank you! – Jonas Hammerschmidt Jun 22 '20 at 15:14

0 Answers0