External DLLs not loading in .NET 5

Scot McPherson 6 Reputation points
2021-08-16T05:50:47.667+00:00

Is there something special needed to get a .NET 5 application to load DLLs at runtime?

In any .NET 5 project I am building, and no matter how I reference said DLLs whether to Nuget package manager, or referencing existing assemblies, my applications fail to load any and all externally sources DLLs. These DLLs have been source either through Nuget using the Visual Studio Nuget Package manager, or building from sources myself and targeting .NET5.0 in the project files.

Example error:

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'MySql.Data, Version=8.0.26.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d'. The system cannot find the file specified.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at DarkRift.Server.PluginFactory.AddFile(String file, DependencyResolutionStrategy dependencyResolutionStrategy, String searchedDirectory) in C:\Users\scotm\Desktop\Unity\DarkRift 2.10.1 Sources\DarkRift.Server\PluginFactory.cs:line 164
System.IO.FileNotFoundException: Could not load file or assembly 'MySql.Data, Version=8.0.26.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d'. The system cannot find the file specified.
File name: 'MySql.Data, Version=8.0.26.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d'

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Scot McPherson 6 Reputation points
    2021-08-17T03:39:31.323+00:00

    I solved the problem, or rather I discovered where the problem is located. It is not a .NET problem, but a problem with how the base server application was written, and that it can't resolve dependencies of plugins written for it. It's a peculiarity with .NET Core, but it's not a bug. Just something one needs to be aware of and write code to handle it better.

    the base application that accepts plugins needs to be able to take an argument from the plugin to load the external DLL dependency with something like this:

    AssemblyLoadContext.Default.Resolving += (context,name) => {
    return System.Reflection.Assembly.LoadFrom(Path.Combine("lib", name.Name +".dll"));

    1 person found this answer helpful.