Plugins in .net 5.0 AssemblyLoadContext

Viewed 188

I'm trying to develop a C# application with plugin architecture. For this purpose I take advantage of the main entry point's AssemblyLoadContext:

AssemblyLoadContext currentAssemblyLoadContext = AssemblyLoadContext.GetLoadContext(currentAssembly);

I then scan a certain folder for *.dll files; if they are not already loaded in currentAssemblyLoadContext, then I load them through:

currentAssemblyLoadContext.LoadFromAssemblyPath(pluginFileName)

This works fine, but later, when inspecting the loaded assemblies for types:

assembly.GetTypes()

I get System.Reflection.ReflectionTypeLoadExceptions saying that some types from some assemblies cannot be loaded.

Notably, the offending assemblies involve Entity Framework Design DLLs, such as "Microsoft.EntityFrameworkCore.Relational.Design.dll", "Microsoft.EntityFrameworkCore.SqlServer.Design.dll", etc.

Errors look like this:

Unable to load one or more of the requested types.
Could not load type 'Microsoft.EntityFrameworkCore.Metadata.RelationalModelAnnotations' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=5.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
Could not load type 'Microsoft.EntityFrameworkCore.Metadata.RelationalPropertyAnnotations' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=5.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
Could not load type 'Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalFullAnnotationNames' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=5.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

To be noted that the assemblies reported in the error (like "Microsoft.EntityFrameworkCore.Relational" in this case) were loaded.

Any suggestions about how to correctly load dlls and types through code at runtime?

0 Answers
Related