完善插件组件

This commit is contained in:
FalconFly 2023-12-21 14:37:17 +08:00
parent 5f9dbb238a
commit 251373c6bf
2 changed files with 35 additions and 14 deletions

View File

@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace Falcon.SugarApi.Plugin
{
/// <summary>
/// 插件配置
/// </summary>
public static class PluginOptions
{
/// <summary>
/// 插件安装搜索目录
/// </summary>
public static List<string> PluginPaths { get; private set; } = new List<string>() { "","/plugin" };
/// <summary>
/// 插件配置文件中Swagger定义节点名称
/// </summary>
public static string SwiggerDefincePathName { get; private set; } = "swagger";
/// <summary>
/// 插件配置文件名称
/// </summary>
public static List<string> PluginNames { get; private set; } = new List<string>();
}
}

View File

@ -1,16 +1,10 @@
using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
namespace Falcon.SugarApi.Plugin.Service namespace Falcon.SugarApi.Plugin.Service
{ {
@ -34,15 +28,16 @@ namespace Falcon.SugarApi.Plugin.Service
} }
List<Assembly> plugin = new(); List<Assembly> plugin = new();
var basePath = AppDomain.CurrentDomain.BaseDirectory; var basePath = AppDomain.CurrentDomain.BaseDirectory;
var pluginPath = @"plugin";
foreach(var pn in pluginNames) { foreach(var pn in pluginNames) {
var pf = pn.EndsWith(".dll") ? pn : pn + ".dll"; var pf = pn.EndsWith(".dll") ? pn : pn + ".dll";
pf = Path.IsPathRooted(pf) ? pf : Path.Combine(basePath,pluginPath,pf); if(!Path.IsPathRooted(pf)) {
foreach(var path in PluginOptions.PluginPaths) {
if(!File.Exists(pf)) { var tfp = Path.Combine(basePath,path,pf);
pf = Path.IsPathRooted(pn) ? pf : Path.Combine(basePath,pn); if(File.Exists(tfp)) {
pf = pf.EndsWith(".dll") ? pf : pf + ".dll"; pf = tfp;
break;
}
}
} }
if(!File.Exists(pf)) { if(!File.Exists(pf)) {
throw new FileNotFoundException(pf); throw new FileNotFoundException(pf);
@ -86,7 +81,7 @@ namespace Falcon.SugarApi.Plugin.Service
obj.AddServices(services,config); obj.AddServices(services,config);
} }
catch(Exception ex) { catch(Exception ex) {
throw new Exception($"注册插件{type.FullName}.IServicePlugin.AddServices]方法时发生异常。",ex); throw new Exception($"调用插件{type.FullName}.IServicePlugin.AddServices]方法时发生异常。",ex);
} }
} }
} }