From 5f9dbb238a3422189ddff8eaa676b720f1365b64 Mon Sep 17 00:00:00 2001 From: FalconFly <12919280+falconfly@user.noreply.gitee.com> Date: Mon, 18 Dec 2023 14:35:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=8F=92=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=E5=92=8C=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Plugin/Client/IServiceCollectionExtend.cs | 13 ++--- Falcon.SugarApi/Plugin/Client/Readme.md | 30 ++++++++++- Falcon.SugarApi/Plugin/Readme.md | 13 ++--- .../Service/IServiceCollectionExtend.cs | 54 +++++++++++++++++-- Falcon.SugarApi/Plugin/Service/Readme.md | 21 +++++++- 5 files changed, 109 insertions(+), 22 deletions(-) diff --git a/Falcon.SugarApi/Plugin/Client/IServiceCollectionExtend.cs b/Falcon.SugarApi/Plugin/Client/IServiceCollectionExtend.cs index 81a3b85..156a76a 100644 --- a/Falcon.SugarApi/Plugin/Client/IServiceCollectionExtend.cs +++ b/Falcon.SugarApi/Plugin/Client/IServiceCollectionExtend.cs @@ -3,8 +3,8 @@ using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; +using System.Linq; using System.Reflection; -using System.Runtime.InteropServices; namespace Falcon.SugarApi.Plugin.Client { @@ -17,14 +17,15 @@ namespace Falcon.SugarApi.Plugin.Client /// 注册当前程序集中的控制器 /// /// 服务集合 + /// 要注册的程序集 /// 服务集合 - public static IServiceCollection AddPluginsController(this IServiceCollection services) { - var pluginAssembly = Assembly.GetExecutingAssembly(); - var part = new AssemblyPart(pluginAssembly); + public static IServiceCollection AddPluginsController(this IServiceCollection services,Assembly assembly) { + var part = new AssemblyPart(assembly); services.AddControllers().ConfigureApplicationPartManager(apm => { - if(!apm.ApplicationParts.Contains(part)) { - apm.ApplicationParts.Add(part); + if(apm.ApplicationParts.Any(a => a.Name == part.Name)) { + return; } + apm.ApplicationParts.Add(part); }); return services; } diff --git a/Falcon.SugarApi/Plugin/Client/Readme.md b/Falcon.SugarApi/Plugin/Client/Readme.md index 7749c95..dd21164 100644 --- a/Falcon.SugarApi/Plugin/Client/Readme.md +++ b/Falcon.SugarApi/Plugin/Client/Readme.md @@ -1 +1,29 @@ -## ͻ˷ \ No newline at end of file +## ͻ˷ + +ڲʵIServicePluginӿڣAddServicesעط + +~~~c# + /// + /// ʵע + /// + public class ServicePiugin:IServicePlugin + { + /// + /// + /// + /// 񼯺 + /// ò + /// 񼯺 + public IServiceCollection AddServices(IServiceCollection services,IConfiguration configuration) { + //Ƿ + if(!configuration.GetSection("baseevent").Exists()) { + return services; + } + //ע᱾е + services.AddPluginsController(this.GetType().Assembly); + //ط񼯺 + return services; + } + } +~~~ + diff --git a/Falcon.SugarApi/Plugin/Readme.md b/Falcon.SugarApi/Plugin/Readme.md index 6cebce9..c957bf7 100644 --- a/Falcon.SugarApi/Plugin/Readme.md +++ b/Falcon.SugarApi/Plugin/Readme.md @@ -8,13 +8,8 @@ ### ʵַʽ 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.1 ͨservices.AddPluginsControllerע + 1.2 ̨ʵBackGroundTaskͨservices.AddHostedServiceעᵽ񼯺ڡ + + 2. WebAPIģ飺վͨservices.AddPluginServiceע -### ĿǰҪ -1. ޷ҵģעControllerá diff --git a/Falcon.SugarApi/Plugin/Service/IServiceCollectionExtend.cs b/Falcon.SugarApi/Plugin/Service/IServiceCollectionExtend.cs index cfafb95..61250e5 100644 --- a/Falcon.SugarApi/Plugin/Service/IServiceCollectionExtend.cs +++ b/Falcon.SugarApi/Plugin/Service/IServiceCollectionExtend.cs @@ -1,11 +1,14 @@ using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.FileProviders; using System; using System.Collections.Generic; using System.Configuration; +using System.IO; using System.Linq; using System.Reflection; +using System.Reflection.Emit; using System.Text; using System.Threading.Tasks; @@ -29,9 +32,45 @@ namespace Falcon.SugarApi.Plugin.Service if(name == null) { return services; } - foreach(var pluginName in pluginNames) { - var pa = AssemblyLoadHelp.ALCLoad(pluginName); - foreach(Type type in pa.GetTypes()) { + List plugin = new(); + var basePath = AppDomain.CurrentDomain.BaseDirectory; + var pluginPath = @"plugin"; + foreach(var pn in pluginNames) { + + var pf = pn.EndsWith(".dll") ? pn : pn + ".dll"; + pf = Path.IsPathRooted(pf) ? pf : Path.Combine(basePath,pluginPath,pf); + + if(!File.Exists(pf)) { + pf = Path.IsPathRooted(pn) ? pf : Path.Combine(basePath,pn); + pf = pf.EndsWith(".dll") ? pf : pf + ".dll"; + } + if(!File.Exists(pf)) { + throw new FileNotFoundException(pf); + } + var pa = AssemblyLoadHelp.ALCLoad(pf); + if(pa != null) { + plugin.Add(pa); + } + } + services.AddPluginService(config,plugin.ToArray()); + return services; + } + + /// + /// 注册插件。 + /// 查找插件中的IServicePlugin实现,并运行其中的AddServices方法注册和初始化插件 + /// + /// 服务集合 + /// 应用程序配置 + /// 插件程序集数组 + /// 服务集合 + public static IServiceCollection AddPluginService(this IServiceCollection services,IConfiguration config,params Assembly[] plugins) { + var name = typeof(IServicePlugin).FullName; + if(name == null) { + return services; + } + foreach(Assembly ass in plugins) { + foreach(Type type in ass.GetTypes()) { if(type == null || !type.IsPublic || type.GetInterface(name) == null) { continue; } @@ -39,12 +78,17 @@ namespace Falcon.SugarApi.Plugin.Service if(tname == null) { continue; } + if(type.Assembly.CreateInstance(tname) is not IServicePlugin obj) { continue; } - obj.AddServices(services,config); + try { + obj.AddServices(services,config); + } + catch(Exception ex) { + throw new Exception($"注册插件{type.FullName}.IServicePlugin.AddServices]方法时发生异常。",ex); + } } - } return services; } diff --git a/Falcon.SugarApi/Plugin/Service/Readme.md b/Falcon.SugarApi/Plugin/Service/Readme.md index 2bea63b..1de285d 100644 --- a/Falcon.SugarApi/Plugin/Service/Readme.md +++ b/Falcon.SugarApi/Plugin/Service/Readme.md @@ -1 +1,20 @@ -## ˵÷ \ No newline at end of file +## ˵÷ + +ֱӵ·עṩƣλӦóĿ¼pluginĿ¼ڡ + +~~~c# +/// +/// ע +/// +/// 񼯺 +/// Ӧó +private void AddPlugins(IServiceCollection services,IConfiguration configuration) { + try { + services.AddPluginService(configuration,"His.Service.BaseEvent"); + } + catch(Exception ex) { + throw new Exception("עʱ쳣",ex); + } +} +~~~ +