New version of Plugin Framework for .NET adds support for reading plugin catalog configuration from appsettings.json and improves plugin tagging. The release is now available through Nuget: https://www.nuget.org/packages/Weikio.PluginFramework/
For those who are not familiar with Plugin Framework, it is a plugin platform for .NET applications, including ASP.NET Core, Blazor, WPF, Windows Forms and Console apps. It is light-weight and easy to integrate and supports multiple different plugin catalogs, including .NET assemblies, Nuget packages and Roslyn scripts.
Here’s an introduction post about Plugin Framework. InfoQ did a an article about the framework, it’s available from their site.
The 1.1.0 release
I’m especially happy with the support for appsettings.json as it’s the first community contribution (thanks @panoukos41 (github.com)!) and as it also enables new scenarios for the framework. Folder and assembly catalogs can be configured with the following syntax (support for more catalog types is coming in the upcoming versions):
"PluginFramework": {
"Catalogs": [
{
"Type": "Folder",
"Path": "..\\Shared\\Weikio.PluginFramework.Samples.SharedPlugins\\bin\\debug\\netcoreapp3.1"
},
{
"Type": "Assembly",
"Path": ".\\bin\\Debug\\netcoreapp3.1\\WebAppPluginsLibrary.dll"
}
]
}
Sample for this specific feature is available from https://github.com/weikio/PluginFramework/tree/master/samples/WebAppWithAppSettings
New version also improves the plugin tagging. The idea behind plugin tagging is that Plugin Framework can for example scan a folder of dlls and tag the found plugins based on the given criteria. The developer can then use the plugins based on their tags. This is useful in situations where the application supports multiple types of plugins.
Here’s an example where all the found plugins are tagged as “operator”:
var folderPluginCatalog = new FolderPluginCatalog(@"..\..\..\..\Shared\Weikio.PluginFramework.Samples.SharedPlugins\bin\debug\netcoreapp3.1",
type => { type.Implements<IOperator>().Tag("operator"); });
And another part of the same app tags plugins as “dialog”:
var folderCatalog = new FolderPluginCatalog(@"..\..\..\..\WinFormsPluginsLibrary\bin\debug\netcoreapp3.1",
type =>
{
type.Implements<IDialog>()
.Tag("dialog");
});
Now when these plugins are used in the application, they can be fetched using the tags. Here’s an example where the “operator” and “dialog” tags are used:
private void AddCalculationOperators()
{
var allPlugins = _allPlugins.GetByTag("operator");
foreach (var plugin in allPlugins)
{
listBox1.Items.Add(plugin);
}
}
private void AddDialogs()
{
var dialogPlugins = _allPlugins.GetByTag("dialog");
foreach (var dialogPlugin in dialogPlugins)
{
var menuItem = new ToolStripButton(dialogPlugin.Name, null, (o, args) =>
{
var instance = (IDialog) Activator.CreateInstance(dialogPlugin);
instance.Show();
});
dialogPluginsToolStripMenuItem.DropDownItems.Add(menuItem);
}
}
Sample for plugin tagging is available from https://github.com/weikio/PluginFramework/tree/master/samples/WinFormsApp
What’s next?
Work will continue on improving (and adding new) plugin catalogs but the main thing for the next release is the support for .NET 5. This will enable one scenario which hasn’t been that well supported: Using Plugin Framework in WebAssembly version of Blazor.