當前位置: 妍妍網 > 碼農

推薦一個.Net Core輕量級外掛程式框架

2024-02-24碼農

這是之前推薦過的外掛程式框架,一直以來比較多人感興趣和咨詢,有沒有類似框架,今天重新整理發下。

今天給大家推薦一個開源外掛程式架構。在介紹計畫之前,我們了解下什麽是外掛程式架構,它的用處。

現有的軟體開發中,業務越來越復雜,一些大型的計畫版本一直在叠代,程式碼規模越來越大,涉及的人員也越來越多,為了降低計畫的耦合度,減少開發的難度,外掛程式框架就是其中一個解決方案。

使用外掛程式架構可以實作並列開發、降低計畫開發復雜度、有利於計畫進度的把控。

01

計畫簡介

這是一個基於.Net Core 輕量級外掛程式框架,整合非常簡單,兩行程式碼完成整合;支持新增Controller,動態路由 ;熱插拔、易擴充套件、無需資料庫。

02

技術架構

1、跨平台:這是基於.Net Core開發的系統,可以部署在Docker, Windows, Linux, Mac。

2、 執行環境: .NET Core 3.1 (+)

3、開發環境: Visual Studio Community 2019

03

計畫結構

04

系統界面截圖

外掛程式列表、外掛程式管理、外掛程式配置

系統整合

1、NuGet安裝外掛程式

PM> Install-Package PluginCore.AspNetCore

2、Startup.cs 配置

using PluginCore.AspNetCore.Extensions;// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollection services){ services.AddControllers();// 1. 添加 PluginCore services.AddPluginCore();}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.publicvoidConfigure(IApplicationBuilder app, IWebHostEnvironment env){if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting();// 2. 使用 PluginCore app.UsePluginCore(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });}

3、存取 https://localhost/PluginCore/Admin,就可以進入管理後台。

外掛程式開發

1、Nuget引入外掛程式

Install-PackagePluginCore.IPlugins.AspNetCore

2、建立外掛程式

添加 HelloWorldPlugin 類 繼承 BasePlugin

public classHelloWorldPlugin : BasePluginIStartupXPlugin{publicoverride (bool IsSuccess, string Message) AfterEnable() { Console.WriteLine($"{nameof(HelloWorldPlugin)}: {nameof(AfterEnable)}");returnbase.AfterEnable(); }publicoverride (bool IsSuccess, string Message) BeforeDisable() { Console.WriteLine($"{nameof(HelloWorldPlugin)}: {nameof(BeforeDisable)}");returnbase.BeforeDisable(); }publicvoidConfigureServices(IServiceCollection services) { }publicvoidConfigure(IApplicationBuilder app) { app.UseMiddleware<SayHelloMiddleware>(); }publicint ConfigureOrder {get {return2; } }publicint ConfigureServicesOrder {get {return2; } }}

3、外掛程式其他配置

態路由配置

[Route("api/plugins/[controller]")][ApiController]public classUserHelloController : ControllerBase{privatereadonly IUserInfoService _userInfoService;publicUserHelloController(IUserInfoService userInfoService) {this._userInfoService = userInfoService; }public ActionResult Get() { UserInfo userInfo = _userInfoService.FirstOrDefaultAsync(m => !m.IsDeleted).Result; SettingsModel settingsModel = PluginSettingsModelFactory.Create<SettingsModel>("HelloWorldPlugin");string rtn = $"使用者名稱: {userInfo.UserName}, 建立時間: {userInfo.CreateTime.ToString()}, Hello: {settingsModel.Hello}";return Ok(rtn); }}

05

計畫地址

https://github.com/yiyungent/PluginCore

最後推薦加入我的 帶你從零學習:三層架構與領域驅動設計架構

- End -

分享一套.NetCore從入門到精通視訊教程

點選下方公眾號卡片,關註我

回復「 888 」,免費領取

推薦閱讀

覺得好看 點個在看👇