插件系统开发
This commit is contained in:
parent
50b87ed420
commit
7dc6d60897
63
Falcon.SugarApi/Plugin/FindPluginService.cs
Normal file
63
Falcon.SugarApi/Plugin/FindPluginService.cs
Normal file
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 查找插件服务
|
||||
/// </summary>
|
||||
public static class FindPluginService
|
||||
{
|
||||
/// <summary>
|
||||
/// 查找并增加插件服务
|
||||
/// </summary>
|
||||
/// <param name="assemblyFile">程序集名称</param>
|
||||
/// <param name="services">服务集合</param>
|
||||
/// <param name="configuration">配置</param>
|
||||
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);
|
||||
}
|
||||
/// <summary>
|
||||
/// 查找并增加插件服务
|
||||
/// </summary>
|
||||
/// <param name="assembly">程序集</param>
|
||||
/// <param name="services">服务集合</param>
|
||||
/// <param name="configuration">配置</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
Falcon.SugarApi/Plugin/IServicePlugin.cs
Normal file
19
Falcon.SugarApi/Plugin/IServicePlugin.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Falcon.SugarApi.Plugin
|
||||
{
|
||||
/// <summary>
|
||||
/// 插件接口。插件通过实现此接口注册服务、配置、后台线程等。
|
||||
/// </summary>
|
||||
public interface IServicePlugin
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册服务
|
||||
/// </summary>
|
||||
/// <param name="services">服务集合</param>
|
||||
/// <param name="configuration">配置</param>
|
||||
/// <returns>服务集合</returns>
|
||||
IServiceCollection AddServices(IServiceCollection services,IConfiguration configuration);
|
||||
}
|
||||
}
|
19
Falcon.SugarApi/Plugin/PluginSetting.cs
Normal file
19
Falcon.SugarApi/Plugin/PluginSetting.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Falcon.SugarApi.Plugin
|
||||
{
|
||||
/// <summary>
|
||||
/// 插件设置
|
||||
/// </summary>
|
||||
public class PluginSetting
|
||||
{
|
||||
/// <summary>
|
||||
/// 插件名称
|
||||
/// </summary>
|
||||
public string name { get; set; }
|
||||
/// <summary>
|
||||
/// 插件文件
|
||||
/// </summary>
|
||||
public string file { get; set; }
|
||||
}
|
||||
}
|
9
Falcon.SugarApi/Plugin/PluginSettingList.cs
Normal file
9
Falcon.SugarApi/Plugin/PluginSettingList.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Falcon.SugarApi.Plugin
|
||||
{
|
||||
/// <summary>
|
||||
/// 插件设置列表
|
||||
/// </summary>
|
||||
public class PluginSettingList:List<PluginSetting> { }
|
||||
}
|
20
Falcon.SugarApi/Plugin/Readme.md
Normal file
20
Falcon.SugarApi/Plugin/Readme.md
Normal file
|
@ -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,不起作用。
|
Loading…
Reference in New Issue
Block a user