在构建WebApplication对象过程中装载中间件

This commit is contained in:
FunCoder 2025-04-22 16:01:08 +08:00
parent 12b7be32fb
commit 2bcaf6375d
3 changed files with 45 additions and 2 deletions

View File

@ -41,6 +41,16 @@ public class Startup : AppStartup
{ {
// 比较版本号对数据库进行升级结构、种子数据等 // 比较版本号对数据库进行升级结构、种子数据等
} }
/// <summary>
/// 构建 WebApplication 对象过程中装载中间件
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
/// <param name="componentContext"></param>
public void LoadAppComponent(IApplicationBuilder app, IWebHostEnvironment env, ComponentContext componentContext)
{
WebApplication application = app as WebApplication;
}
} }
/// <summary> /// <summary>

View File

@ -429,7 +429,7 @@ public static class SqlSugarSetup
{ {
// 扫描所有继承 AppStartup 的类(排序执行顺序) // 扫描所有继承 AppStartup 的类(排序执行顺序)
var startups = App.EffectiveTypes var startups = App.EffectiveTypes
.Where(u => typeof(AppStartup).IsAssignableFrom(u) && u.IsClass && !u.IsAbstract && !u.IsGenericType && u.GetMethod("BeforeInitTable") != null) .Where(u => typeof(AppStartup).IsAssignableFrom(u) && u.IsClass && !u.IsAbstract && !u.IsGenericType && (u.GetMethod("BeforeInitTable") != null || u.GetMethod("AfterInitSeed") != null))
.OrderByDescending(u => !u.IsDefined(typeof(AppStartupAttribute), true) ? 0 : u.GetCustomAttribute<AppStartupAttribute>(true).Order); .OrderByDescending(u => !u.IsDefined(typeof(AppStartupAttribute), true) ? 0 : u.GetCustomAttribute<AppStartupAttribute>(true).Order);
if (startups == null || !startups.Any()) return (startups, 0, 0); if (startups == null || !startups.Any()) return (startups, 0, 0);

View File

@ -5,8 +5,10 @@
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Furion;
using System.Reflection;
Serve.Run(RunOptions.Default.AddWebComponent<WebComponent>()); Serve.Run(RunOptions.Default.AddWebComponent<WebComponent>().UseComponent<ApplicationComponent>());
public class WebComponent : IWebComponent public class WebComponent : IWebComponent
{ {
@ -32,3 +34,34 @@ public class WebComponent : IWebComponent
}); });
} }
} }
public class ApplicationComponent : IApplicationComponent
{
/// <summary>
/// 构建 WebApplication 对象过程中装载中间件
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
/// <param name="componentContext"></param>
public void Load(IApplicationBuilder app, IWebHostEnvironment env, ComponentContext componentContext)
{
//WebApplication application = app as WebApplication;
// 扫描所有继承 AppStartup 的类(排序执行顺序)
var startups = App.EffectiveTypes
.Where(u => typeof(AppStartup).IsAssignableFrom(u) && u.IsClass && !u.IsAbstract && !u.IsGenericType && u.GetMethod("LoadAppComponent") != null)
.OrderByDescending(u => !u.IsDefined(typeof(AppStartupAttribute), true) ? 0 : u.GetCustomAttribute<AppStartupAttribute>(true).Order);
if (startups == null || !startups.Any())
return;
try
{
foreach (var type in startups)
{
var startup = Activator.CreateInstance(type) as AppStartup;
var initDataMethod = type.GetMethod("LoadAppComponent");
initDataMethod?.Invoke(startup, [app, env, componentContext]);
}
}
catch (Exception ex)
{
}
}
}