PluginPlatform.UI 1.0.15

dotnet add package PluginPlatform.UI --version 1.0.15
                    
NuGet\Install-Package PluginPlatform.UI -Version 1.0.15
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PluginPlatform.UI" Version="1.0.15" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PluginPlatform.UI" Version="1.0.15" />
                    
Directory.Packages.props
<PackageReference Include="PluginPlatform.UI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PluginPlatform.UI --version 1.0.15
                    
#r "nuget: PluginPlatform.UI, 1.0.15"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PluginPlatform.UI@1.0.15
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PluginPlatform.UI&version=1.0.15
                    
Install as a Cake Addin
#tool nuget:?package=PluginPlatform.UI&version=1.0.15
                    
Install as a Cake Tool

PluginPlatform

为 ASP.NET Core 应用提供热插拔插件运行时:独立程序集按插件自有 AssemblyLoadContext 隔离加载/卸载,自动发现 Controller、中间件、过滤器、后台任务与静态资源,并附带一套 FreeSql + SQLite 驱动的可视化管理控制台。

NuGet Version NuGet Version NuGet Version Target Framework License: MIT

Why This Exists

单体应用迭代越久,编译启动越慢、发布越重、团队越容易互相阻塞。PluginPlatform 把"功能模块"从主程序里拆成可独立编译、独立部署、独立卸载的插件程序集:

  • 插件在独立的 AssemblyLoadContext 中加载,与主程序共享白名单内的宿主 DLL(如 Microsoft.AspNetCore、System.、PluginPlatform.),其余完全隔离——这意味着插件可以热卸载、不污染主程序。
  • 插件里的 Controller / 中间件 / 过滤器 / IHostedService / 静态资源会被自动发现并注册,你不必手动 app.UseXXX()。
  • PluginPlatform.UI 提供开箱即用的管理控制台 + REST API + SQLite 持久化,上传一个 zip 即可上线一个插件,无需改主程序一行代码。

包构成

包 职责 依赖
PluginPlatform.Core 插件基类 PluginBase、中间件构造器、过滤器基类(最底层,无内部依赖) ASP.NET Core 抽象、DI 抽象
PluginPlatform 插件加载器 PluginLoader、控制器/中间件/过滤器/静态/后台任务管理器、宿主接入扩展 PluginPlatform.Core
PluginPlatform.UI 可视化管理控制台、REST API、FreeSql+SQLite 持久化 PluginPlatform、FreeSql.Sqlite、SharpZipLib

Quick Start

在 ASP.NET Core 主机项目中安装两个包:

dotnet add package PluginPlatform
dotnet add package PluginPlatform.UI

在 Program.cs 中接入(这是最短可用路径,无需任何配置):

using PluginPlatform;
using PluginPlatform.UI;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddPluginPlatform();      // 注册插件运行时
builder.Services.AddPluginPlatformUI();    // 注册管理控制台与持久化

var app = builder.Build();

app.UsePluginPlatform();                    // 启用插件中间件与静态资源
app.UsePluginPlatformUI();                  // 启动时按 SQLite 记录自动加载已启用插件

app.Run();

启动后,管理控制台(上传/启用/停用插件)暴露在:

GET /api/PluginPlatform/UI/Index

Installation

前置条件:.NET 8 SDK 及以上。

# 运行时本体(必装)
dotnet add package PluginPlatform

# 可视化管理控制台 + REST API(按需)
dotnet add package PluginPlatform.UI

# 仅开发插件、不需要宿主接入时引用基础库
dotnet add package PluginPlatform.Core

包目标框架为 net8.0,兼容任意 ASP.NET Core 8 主机。

Usage

编写一个插件

新建一个类库项目,引用 PluginPlatform.Core,实现一个继承 PluginBase 的类:

using Microsoft.Extensions.DependencyInjection;
using PluginPlatform.Core;

public class HelloPlugin : PluginBase
{
    // 向插件自己的 DI 容器注册服务(宿主服务可通过 hostServiceProvider 获取)
    public override void ConfigureServices(IServiceCollection services, IServiceProvider hostServiceProvider)
    {
        services.AddSingleton<GreetingService>();
    }

    // 注册中间件(可选)
    public override void Configure(MiddlewareBuilder app)
    {
        app.Use(async (ctx, next) =>
        {
            ctx.Response.Headers["X-Plugin"] = "HelloPlugin";
            await next();
        });
    }
}

public class GreetingService
{
    public string Say() => "Hello from plugin!";
}

插件程序集里的以下类型会被自动发现:

  • ControllerBase 的子类 → 注册为 MVC 控制器(独立 ApplicationPart)
  • PluginFilterAttribute 的子类 → 注册为 MVC 过滤器
  • IHostedService 的子类 → 作为后台任务启动
  • Configure 中注册的中间件 → 注入宿主管道
  • wwwroot 下的静态文件 → 按 PluginOptions.WWWROOTPath 提供

加载插件

方式 A — 编程式(适合嵌入式/测试):

var pluginLoader = app.Services.GetRequiredService<PluginLoader>();
pluginLoader.LoadPlugin(
    pluginId: "hello",
    assemblyPath: Path.Combine(Directory.GetCurrentDirectory(), "plugins", "hello", "hello.dll"),
    options: new PluginOptions { WWWROOTPath = "hello" });

方式 B — 通过管理控制台上传 zip(详见 操作指南)。zip 内需包含 plugin.json 清单与插件 DLL,例如:

{
  "id": "hello",
  "name": "Hello Plugin",
  "main": "hello.dll",
  "version": "1.0.0"
}

Configuration

PluginOptions 控制单个插件的加载行为:

选项 类型 默认值 说明
WWWROOTPath string Plugins/{PluginId} 插件静态资源在宿主 wwwroot 下的挂载路径
ShareDllWhitelist List<string> ["Microsoft.AspNetCore", "System.", "PluginPlatform."] 与宿主共享(不隔离)的程序集前缀白名单;命中前缀的 DLL 复用宿主版本,避免重复加载与类型冲突

管理控制台(REST API)

PluginPlatform.UI 提供以下端点(路由前缀 api/PluginPlatform/UI):

方法 路径 说明
GET /Index 内置管理控制台 HTML
GET /GetPluginRecords 列出全部插件记录
POST /Upload 上传插件 zip(表单字段 file)
PUT /ChangeStatus?id=<id>&enable=<bool> 启用/停用插件(触发加载/卸载)
DELETE /Remove?id=<id> 删除插件记录与文件

完整字段与返回结构见 REST API 参考。

架构与原理

  • 隔离模型:每个插件运行在独立的 AssemblyLoadContext,白名单内的宿主 DLL 共享,其余隔离,从而支持卸载与热更新。详见 架构总览。
  • 生命周期:LoadPlugin 按顺序注册中间件→控制器→过滤器→静态资源→后台任务,并触发 PluginAfterActiveCallback;UnloadPlugin 先触发 PluginBeforeReleaseCallback 再回滚全部注册并强制 GC。

API Reference

公开 API 的完整定义见文档站 参考 分组(REST API 参考 / 插件清单 plugin.json)。源码中的 /// 注释同步维护,作为 IDE IntelliSense 实时提示:

  • PluginPlatform.PluginPlatformExpansion — AddPluginPlatform / UsePluginPlatform
  • PluginPlatform.UI.PluginPlatformUIExpansion — AddPluginPlatformUI / UsePluginPlatformUI
  • PluginPlatform.PluginLoader — LoadPlugin / UnloadPlugin / GetPluginContainer
  • PluginPlatform.Core.PluginBase — 插件基类
  • PluginPlatform.PluginOptions — 加载选项
  • PluginPlatform.UI.PluginPlatformUIService — 管理服务的增删改查

文档站构建方式见仓库 CONTRIBUTING.md。

Contributing

欢迎贡献。文档与代码遵循 文档体系规范,PR 需通过文档构建检查。详见 CONTRIBUTING.md。

License

MIT © tanch — 见 LICENSE。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.15 164 8/7/2026
1.0.14 121 8/6/2026
1.0.13 146 2/9/2026
1.0.12 134 2/9/2026
1.0.11 357 9/19/2025
1.0.10 233 9/9/2025
1.0.2 188 7/29/2025
1.0.1 205 7/28/2025
1.0.0 204 7/28/2025