Search code examples
c#reflectionentity-framework-core.net-6.0

Hot-patching "deps.json" at runtime?


I have a .NET 6.0 DLL that contains an Entity Framework Core DbContext to a SQL Server database.

If that assembly is referenced by the .NET 6.0 EXE and then called, everything works just fine.

However, if I try to use it in a plug-in concept and load the DLL via reflection like this:

var assembly = Assembly.LoadFrom("Plugin.dll");
var pluginType = assembly.GetExportedTypes()
  .Where(t => t.IsAssignableTo(typeof(IPlugin)))
  .First();
var plugin = (IPlugin)Activator.CreateInstance(pluginType);

Then I get

Microsoft.Data.SqlClient is not supported on this platform.

And the only difference is in the PluginHost.deps.json that now has no clue that at some point Microsoft.Data.SqlClient may get involved.

PluginHost.deps.json

And I was wondering if there was some hot-load function to teach the EXE the dependencies of the DLL.

The whole test endeavor can be found on GitHub


Solution

  • I found the answer by this blog post and the nuget provided therein.

    In essence: Add the nuget McMaster.NETCore.Plugins to your plugin host and then load the plugin like this:

    var pluginInterfaceType = typeof(IPlugin); //must be shared between host and plugin
    var loader = PluginLoader.CreateFromAssemblyFile(
        assemblyFile: fullPathAndFileNameToPlugin,
        sharedTypes: new[] { pluginInterfaceType });
    var assembly = loader.LoadDefaultAssembly();
    var pluginType = assembly.GetExportedTypes()
        .Where(t => t.IsAssignableTo(pluginInterfaceType))
        .First();
    var plugin = (IPlugin)Activator.CreateInstance(pluginType);