Xfrogcn.PluginFactory.Abstractions
1.0.0
See the version list below for details.
dotnet add package Xfrogcn.PluginFactory.Abstractions --version 1.0.0
NuGet\Install-Package Xfrogcn.PluginFactory.Abstractions -Version 1.0.0
<PackageReference Include="Xfrogcn.PluginFactory.Abstractions" Version="1.0.0" />
paket add Xfrogcn.PluginFactory.Abstractions --version 1.0.0
#r "nuget: Xfrogcn.PluginFactory.Abstractions, 1.0.0"
// Install Xfrogcn.PluginFactory.Abstractions as a Cake Addin #addin nuget:?package=Xfrogcn.PluginFactory.Abstractions&version=1.0.0 // Install Xfrogcn.PluginFactory.Abstractions as a Cake Tool #tool nuget:?package=Xfrogcn.PluginFactory.Abstractions&version=1.0.0
.NET Core 插件框架
pluginfactory 是 .NET Core 下基于依赖注入实现的插件框架,此框架是插件化开发与依赖注入的完美集合,同时融入了 .NET Core 中的配置机制,可以很好地与 ASP.NET Core 等框架融合。
使用向导
示例项目可参考:Xfrogcn.PluginFactory.Example
Gitee地址 Github地址
安装
在主程序项目中添加Xfrogcn.PluginFactory
包
```dotnet
dotnet add package Xfrogcn.PluginFactory --version 1.0.0
```
在插件项目中添加Xfrogcn.PluginFactory.Abstractions
包
```dotnet
dotnet add package Xfrogcn.PluginFactory.Abstractions --version 1.0.0
```
在主程序中启用
可通过以下两种方式来启用插件库,一是通过在Host
层级的Use机制以及在依赖注入IServiceCollection
层级的Add机制,以下分别说明:
通过IHostBuilder
的UsePluginFactory
方法启用插件库
```c#
var builder = Host.CreateDefaultBuilder(args);
builder.UsePluginFactory();
```
UsePluginFactory
具有多个重载版本,详细请查看配置文档
默认配置下,将使用程序运行目录下的Plugins
目录作为插件程序集目录, 使用宿主配置文件作为插件配置文件(通常为appsettings.json)
你也可以通过使用带有Assembly
或IEnumerable<Assembly>
参数的版本直接传入插件所在的程序集
通过IServiceCollection
的AddPluginFactory
方法启用插件库
```c#
var builder = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddPluginFactory();
});
```
AddPluginFactory
具有多个重载版本,详细请查看配置文档
默认配置下,将使用程序运行目录下的Plugins
目录作为插件程序集目录
注意:
AddPluginFactory方法不会
使用默认的配置文件作为插件配置,你需要显式地传入IConfiguration
, 如果是在 ASP.NET Core 环境中,你可以在Startup类中直接获取到
编写插件
插件是实现了IPlugin接口的类,在插件库中也提供了PluginBase基类,一般从此类继承即可。标准插件具有启动和停止方法,通过IPluginFactory
进行控制。
要编写插件,一般遵循以下步骤:
创建插件项目(.NET Core 类库),如TestPluginA
添加
Xfrogcn.PluginFactory.Abstractions
包```nuget dotnet add package Xfrogcn.PluginFactory.Abstractions ```
创建插件类,如Plugin,从PluginBase继承
```c# public class Plugin : PluginBase { public override Task StartAsync(IPluginContext context) { Console.WriteLine("插件A已启动"); return base.StartAsync(context); } public override Task StopAsync(IPluginContext context) { Console.WriteLine("插件B已启动"); return base.StopAsync(context); } } ```
启动或停止方法中可通过context中的ServiceProvider获取注入服务
通过
PluginAttribute
特性设置插件的元数据```c# [Plugin(Alias = "PluginA", Description = "测试插件")] public class Plugin : PluginBase { } ```
插件元数据以及插件载入的插件列表信息可以通过
IPluginLoader.PluginList
获取
插件启动
IPluginFactory
本身实现了.NET Core扩展库的IHostedService
机制,故如果你是在宿主环境下使用,如(ASP.NET Core),插件的启动及停止将自动跟随宿主进行
如果未使用宿主,可通过获取IPluginFactory
实例调用相应方法来完成
```c#
// 手动启动
var pluginFactory = provider.GetRequiredService<IPluginFactory>();
await pluginFactory.StartAsync(default);
await pluginFactory.StopAsync(default);
```
编写支持初始化的插件
在很多场景,我们需要在插件中控制宿主的依赖注入,如注入新的服务等,这时候我们可通过实现支持初始化的插件(ISupportInitPlugin
)来实现,该接口的Init
方法将在依赖注入构建之前调用,通过方法参数IPluginInitContext
中的ServiceCollection
可以控制宿主注入容器。
```c#
[Plugin(Alias = "PluginA", Description = "测试插件")]
public class Plugin : PluginBase, ISupportInitPlugin
{
public void Init(IPluginInitContext context)
{
// 注入服务
//context.ServiceCollection.TryAddScoped<ICustomerService>();
}
}
```
使用插件配置
插件支持 .NET Core 扩展库中的Options及Configuration机制,你只需要从SupportConfigPluginBase<TOptions>
类继承实现插件即可,其中TOptions泛型为插件的配置类型。插件配置自动从宿主配置或启用插件工厂时传入的配置中获取,插件配置位于配置下的Plugins节点,该节点下以插件类名称或插件别名(通过PluginAttribute
特性指定)作为键名,此键之下为插件的配置,如以下配置文件:
```appsettings.json
{
"Plugins": {
"PluginA": {
"TestConfig": "Hello World"
},
}
}
```
扩展PluginA实现配置:
定义配置类,如PluginOptions
```c# public class PluginOptions { public string TestConfig { get; set; } } ```
实现插件
```c# [Plugin(Alias = "PluginA", Description = "测试插件")] public class Plugin : SupportConfigPluginBase<PluginOptions>, ISupportInitPlugin { public Plugin(IOptionsMonitor<PluginOptions> options) : base(options) { } public void Init(IPluginInitContext context) { // 注入服务 //context.ServiceCollection.TryAddScoped<ICustomerService>(); Console.WriteLine($"Init 插件配置:{Options.TestConfig}"); } public override Task StartAsync(IPluginContext context) { Console.WriteLine("插件A已启动"); Console.WriteLine($"StartAsync 插件配置:{Options.TestConfig}"); return base.StartAsync(context); } public override Task StopAsync(IPluginContext context) { Console.WriteLine("插件B已停止"); return base.StopAsync(context); } ```
注意:在插件初始化方法中也可使用注入的配置
跨插件配置
有些配置可能需要在多个插件中共享,此时你可通过Plugins
下的_Share
节点进行配置,此节点下配置将会被合并到插件配置中,可通过PluginOptions进行访问。
```appsettings.json
{
"Plugins": {
"PluginA": {
},
"_Share": {
"TestConfig": "Hello World"
}
}
}
```
插件化 ASP.NET Core
要让 ASP.NET Core 获取得到插件中的控制器,你只需要在插件的初始化方法Init
中,向MVC注入插件程序集:
```c#
context.ServiceCollection.AddMvcCore()
.AddApplicationPart(typeof(Plugin).Assembly);
```
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.7)
- Microsoft.Extensions.FileProviders.Physical (>= 3.1.7)
- Microsoft.Extensions.Hosting.Abstractions (>= 3.1.7)
- Microsoft.Extensions.Options (>= 3.1.7)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 3.1.7)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Xfrogcn.PluginFactory.Abstractions:
Package | Downloads |
---|---|
Xfrogcn.PluginFactory
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.1 | 604 | 7/1/2021 |
2.0.0 | 605 | 11/16/2020 |
1.0.0 | 726 | 11/2/2020 |
1.0.0-beta2020091101 | 354 | 9/11/2020 |