新增模块注册
This commit is contained in:
parent
44100f9cf9
commit
a17d7697e6
35
Falcon.SugarApi.Test/ModuleRegisterTest.cs
Normal file
35
Falcon.SugarApi.Test/ModuleRegisterTest.cs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
using Falcon.SugarApi.ApiDefinistions;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Falcon.SugarApi.Test
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 模块注册测试
|
||||||
|
/// </summary>
|
||||||
|
[TestClass]
|
||||||
|
public class ModuleRegisterTest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 注册测试
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void RegisterTest() {
|
||||||
|
var services = new ServiceCollection() as IServiceCollection;
|
||||||
|
var cb = new ConfigurationBuilder();
|
||||||
|
var config = cb.Build() as IConfiguration;
|
||||||
|
ModuleRegister.ModuleServiceRegistration(services,"aaa",config);
|
||||||
|
var ab = new ApplicationBuilder(services.BuildServiceProvider());
|
||||||
|
ModuleRegister.ApplicationBuilder(ab,"aaa",config);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
Falcon.SugarApi/ApiDefinistions/IModuleRegister.cs
Normal file
31
Falcon.SugarApi/ApiDefinistions/IModuleRegister.cs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Falcon.SugarApi.ApiDefinistions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 模块注册接口
|
||||||
|
/// </summary>
|
||||||
|
public interface IModuleRegister
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 服务注册
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services">服务结合</param>
|
||||||
|
/// <param name="configuration">配置</param>
|
||||||
|
/// <returns>服务集合</returns>
|
||||||
|
public IServiceCollection ServiceRegister(IServiceCollection services,IConfiguration configuration);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 应用注册
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="application">应用创建器</param>
|
||||||
|
/// <param name="configuration">配置</param>
|
||||||
|
/// <returns>应用创建器</returns>
|
||||||
|
public IApplicationBuilder ApplicationRegister(IApplicationBuilder application,IConfiguration configuration);
|
||||||
|
}
|
||||||
|
}
|
63
Falcon.SugarApi/ApiDefinistions/ModuleRegister.cs
Normal file
63
Falcon.SugarApi/ApiDefinistions/ModuleRegister.cs
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Falcon.SugarApi.ApiDefinistions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 模块注册
|
||||||
|
/// </summary>
|
||||||
|
public static class ModuleRegister
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 模块服务注册
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services">服务集合</param>
|
||||||
|
/// <param name="moduleName">模块名称</param>
|
||||||
|
/// <param name="configuration">配置</param>
|
||||||
|
/// <returns>服务集合</returns>
|
||||||
|
/// <exception cref="Exception"></exception>
|
||||||
|
public static IServiceCollection ModuleServiceRegistration(IServiceCollection services,string moduleName,IConfiguration configuration) {
|
||||||
|
foreach(var mr in GetModuleRegister(moduleName)) {
|
||||||
|
mr.ServiceRegister(services,configuration);
|
||||||
|
}
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 应用注册
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="application">应用创建器</param>
|
||||||
|
/// <param name="moduleName">模块名称</param>
|
||||||
|
/// <param name="configuration">配置</param>
|
||||||
|
/// <returns>应用创建器</returns>
|
||||||
|
public static IApplicationBuilder ApplicationBuilder(IApplicationBuilder application,string moduleName,IConfiguration configuration) {
|
||||||
|
foreach(var mr in GetModuleRegister(moduleName)) {
|
||||||
|
mr.ApplicationRegister(application,configuration);
|
||||||
|
}
|
||||||
|
return application;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<IModuleRegister> GetModuleRegister(string moduleName) {
|
||||||
|
Assembly ass;
|
||||||
|
try {
|
||||||
|
ass = Assembly.Load(moduleName);
|
||||||
|
}
|
||||||
|
catch(Exception ex) {
|
||||||
|
throw new Exception($"获取程序集{moduleName}错误!",ex);
|
||||||
|
}
|
||||||
|
var types = ass.GetTypes();
|
||||||
|
foreach(var type in types) {
|
||||||
|
if(type.GetInterface(typeof(IModuleRegister).FullName) != null) {
|
||||||
|
var obj = type.Assembly.CreateInstance(type.FullName);
|
||||||
|
if(obj is IModuleRegister mr) {
|
||||||
|
yield return mr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user