From 7dc6d608979625bed7d952cd24351d045a5dcb3e Mon Sep 17 00:00:00 2001 From: FalconFly <12919280+falconfly@user.noreply.gitee.com> Date: Fri, 10 Nov 2023 11:05:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E7=B3=BB=E7=BB=9F=E5=BC=80?= =?UTF-8?q?=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi/Plugin/FindPluginService.cs | 63 +++++++++++++++++++++ Falcon.SugarApi/Plugin/IServicePlugin.cs | 19 +++++++ Falcon.SugarApi/Plugin/PluginSetting.cs | 19 +++++++ Falcon.SugarApi/Plugin/PluginSettingList.cs | 9 +++ Falcon.SugarApi/Plugin/Readme.md | 20 +++++++ 5 files changed, 130 insertions(+) create mode 100644 Falcon.SugarApi/Plugin/FindPluginService.cs create mode 100644 Falcon.SugarApi/Plugin/IServicePlugin.cs create mode 100644 Falcon.SugarApi/Plugin/PluginSetting.cs create mode 100644 Falcon.SugarApi/Plugin/PluginSettingList.cs create mode 100644 Falcon.SugarApi/Plugin/Readme.md diff --git a/Falcon.SugarApi/Plugin/FindPluginService.cs b/Falcon.SugarApi/Plugin/FindPluginService.cs new file mode 100644 index 0000000..5c84167 --- /dev/null +++ b/Falcon.SugarApi/Plugin/FindPluginService.cs @@ -0,0 +1,63 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.IO; +using System.Reflection; +using System.Runtime.Loader; + +namespace Falcon.SugarApi.Plugin +{ + /// + /// 查找插件服务 + /// + public static class FindPluginService + { + /// + /// 查找并增加插件服务 + /// + /// 程序集名称 + /// 服务集合 + /// 配置 + public static void FindAddPluginService(string assemblyFile,IServiceCollection services,IConfiguration configuration) { + if(!File.Exists(assemblyFile)) { + throw new FileNotFoundException($"插件文件没有找到:{assemblyFile}"); + } + //var doMain = AppDomain.CurrentDomain; + //var assembly = doMain.Load(assemblyFile); + + var dm = new AssemblyLoadContext(null,false); + var assembly = dm.LoadFromAssemblyPath(assemblyFile); + + FindAddPluginService(assembly,services,configuration); + } + /// + /// 查找并增加插件服务 + /// + /// 程序集 + /// 服务集合 + /// 配置 + public static void FindAddPluginService(Assembly assembly,IServiceCollection services,IConfiguration configuration) { + var name = typeof(IServicePlugin).FullName; + if(name == null) { + return; + } + foreach(Type type in assembly.GetTypes()) { + if(type == null || !type.IsPublic) { + continue; + } + if(type.GetInterface(name) == null) { + continue; + } + var tname = type.FullName; + if(tname == null) { + continue; + } + var obj = type.Assembly.CreateInstance(tname) as IServicePlugin; + if(obj == null) { + continue; + } + obj.AddServices(services,configuration); + } + } + } +} diff --git a/Falcon.SugarApi/Plugin/IServicePlugin.cs b/Falcon.SugarApi/Plugin/IServicePlugin.cs new file mode 100644 index 0000000..4230959 --- /dev/null +++ b/Falcon.SugarApi/Plugin/IServicePlugin.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Falcon.SugarApi.Plugin +{ + /// + /// 插件接口。插件通过实现此接口注册服务、配置、后台线程等。 + /// + public interface IServicePlugin + { + /// + /// 注册服务 + /// + /// 服务集合 + /// 配置 + /// 服务集合 + IServiceCollection AddServices(IServiceCollection services,IConfiguration configuration); + } +} diff --git a/Falcon.SugarApi/Plugin/PluginSetting.cs b/Falcon.SugarApi/Plugin/PluginSetting.cs new file mode 100644 index 0000000..345b8ce --- /dev/null +++ b/Falcon.SugarApi/Plugin/PluginSetting.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; + +namespace Falcon.SugarApi.Plugin +{ + /// + /// 插件设置 + /// + public class PluginSetting + { + /// + /// 插件名称 + /// + public string name { get; set; } + /// + /// 插件文件 + /// + public string file { get; set; } + } +} diff --git a/Falcon.SugarApi/Plugin/PluginSettingList.cs b/Falcon.SugarApi/Plugin/PluginSettingList.cs new file mode 100644 index 0000000..4976003 --- /dev/null +++ b/Falcon.SugarApi/Plugin/PluginSettingList.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Falcon.SugarApi.Plugin +{ + /// + /// 插件设置列表 + /// + public class PluginSettingList:List { } +} diff --git a/Falcon.SugarApi/Plugin/Readme.md b/Falcon.SugarApi/Plugin/Readme.md new file mode 100644 index 0000000..6cebce9 --- /dev/null +++ b/Falcon.SugarApi/Plugin/Readme.md @@ -0,0 +1,20 @@ +## ģ + +### Ŀ + +1. ϣҵģԲķʽС +1. ĿȻ󽫱DLLļĿ¼. +1. վʱͨļ̬زضע᷽עᡣ + +### ʵַʽ + 1. ҵģ飺ʵIServicePiuginӿʵֲģעᡣ + 1.1 עControllerδҵע᷽ + 1.2 ע̨BackGroundTaskʵ֣עᵽ񼯺ڡ + 1.3 עOptionsࡣ + 2. ģ飺λFalcon.SugarApi.Pluginڡ + 2.1 IServicePiuginӿڡ + 2.2 FindPluginServiceطڷҵģIServicePiuginʵ֡ + 3. WebAPIģ飺վFindPluginServiceطעҵģĴעᡣ + +### ĿǰҪ +1. ޷ҵģעControllerá