[BREAKGLASS] Append-only mirror of github.com/btcpayserver/DotNetCorePlugins
Go to file
2019-06-21 14:03:58 -07:00
.github Update stale.yml 2019-05-23 08:38:34 -07:00
.vscode Fix #12 - Give base-path the lowest precedence when searching for managed libraries 2018-10-20 16:49:07 -07:00
docs Drop support for configuring plugins via xml file and refactor PluginLoader.Create* APIs 2019-04-24 22:00:18 -07:00
samples Drop support for configuring plugins via xml file and refactor PluginLoader.Create* APIs 2019-04-24 22:00:18 -07:00
src Allow loading assembly by path (#44) 2019-06-21 13:55:57 -07:00
test Enforce build order of test project refs 2019-06-21 14:03:58 -07:00
.editorconfig Replace AppVeyor with Azure Pipelines (#29) 2019-01-24 19:19:57 -08:00
.gitignore Update SDK to 3.0.100-preview5-011568 2019-05-08 22:49:08 -07:00
azure-pipelines.yml Update build to 3.0 preview6 2019-06-21 13:40:07 -07:00
build.ps1 Replace AppVeyor with Azure Pipelines (#29) 2019-01-24 19:19:57 -08:00
Directory.Build.props Add support for unloadable plugins 2019-03-25 20:25:25 -07:00
Directory.Build.targets Closes #32 - PluginLoader can be created with an existing PluginConfig (#40) 2019-04-21 08:27:24 -07:00
DotNetCorePlugins.sln Drop support for configuring plugins via xml file and refactor PluginLoader.Create* APIs 2019-04-24 22:00:18 -07:00
LICENSE.txt Add license and copyright 2018-07-23 21:12:22 -07:00
README.md Drop support for configuring plugins via xml file and refactor PluginLoader.Create* APIs 2019-04-24 22:00:18 -07:00
releasenotes.props Drop support for configuring plugins via xml file and refactor PluginLoader.Create* APIs 2019-04-24 22:00:18 -07:00
version.props Add support for unloadable plugins 2019-03-25 20:25:25 -07:00

.NET Core Plugins

Build Status

NuGet MyGet

This project provides API for loading .NET Core assemblies dynamically, executing them as extensions to the main application, and finding and isolating the dependencies of the plugin from the main application.

Unlike other approaches to dynamic assembly loading, like Assembly.LoadFrom, this API attempts to imitate the behavior of .deps.json and runtimeconfig.json files to probe for dependencies, load native (unmanaged) libraries, and to find binaries from runtime stores or package caches. In addition, it allows for fine-grained control over which types should be unified between the loader and the plugin, and which can remain isolated from the main application.

Blog post introducing this project: .NET Core Plugins: Introducing an API for loading .dll files (and their dependencies) as 'plugins'

Getting started

Pre-release builds and symbols: https://www.myget.org/gallery/natemcmaster/

You can install the plugin loading API using the McMaster.NETCore.Plugins NuGet package.

dotnet add package McMaster.NETCore.Plugins

The main API to use is PluginLoader.CreateFromAssemblyFile.

PluginLoader.CreateFromAssemblyFile(
    assemblyFile: "./plugins/MyPlugin/MyPlugin1.dll",
    sharedTypes: new [] { typeof(IPlugin), typeof(IServiceCollection), typeof(ILogger) },
    isUnloadable: true)
  • assemblyFile = the file path to the main .dll of the plugin
  • sharedTypes = a list of types which the loader should ensure are unified
  • isUnloadable = (.NET Core 3+ only). Allow this plugin to be unloaded from memory at some point in the future. (Requires ensuring that you have cleaned up all usages of types from the plugin before unloading actually happens.)

See example projects in samples/ for more detailed, example usage.

Usage

Using plugins requires at least two projects: (1) the 'host' app which loads plugins and (2) the plugin, but typically also uses a third, (3) an abstractions project which defines the interaction between the plugin and the host.

The plugin abstraction

You can define your own plugin abstractions. A minimal plugin might look like this.

public interface IPlugin
{
    string GetName();
}

The plugins

Typically, it is best to implement plugins by targeting netcoreapp2.0 or higher. They can target netstandard2.0 as well, but using netcoreapp2.0 is better because it reduces the number of redundant System.* assemblies in the plugin output.

A minimal implementation of the plugin could be as simple as this.

internal class MyPlugin1 : IPlugin
{
    public string GetName() => "My plugin v1";
}

The host

The host application can load plugins using the PluginLoader API. The host app needs to define a way to find the assemblies for the plugin on disk. One way to do this is to follow a convention, such as:

plugins/
    $PluginName1/
        $PluginName1.dll
        (additional plugin files)
    $PluginName2/
        $PluginName2.dll

For example, you could prepare the sample plugin above by running

dotnet publish MyPlugin1.csproj --output plugins/MyPlugin1/

An implementation of a host which finds and loads this plugin might look like this:

using McMaster.NETCore.Plugins;

public class Program
{
    public static void Main(string[] args)
    {
        var loaders = new List<PluginLoader>();

        // create plugin loaders
        var pluginsDir = Path.Combine(AppContext.BaseDirectory, "plugins");
        foreach (var dir in Directory.GetDirectories(pluginsDir))
        {
            var dirName = Path.GetFileName(dir);
            var pluginDll = Path.Combine(dir, dirName + ".dll");
            if (File.Exists(pluginDll))
            {
                var loader = PluginLoader.CreateFromAssemblyFile(
                    pluginDll,
                    sharedTypes: new [] { typeof(IPlugin) });
                loaders.Add(loader);
            }
        }

        // Create an instance of plugin types
        foreach (var loader in loaders)
        {
            foreach (var pluginType in loader
                .LoadDefaultAssembly()
                .GetTypes()
                .Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract))
            {
                // This assumes the implementation of IPlugin has a parameterless constructor
                IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);

                Console.WriteLine($"Created plugin instance '{plugin.GetName()}'.");
            }
        }
    }
}