概述
命名空间:ZhonTai.Admin.Core
中台 Admin 的启动入口是 HostApp 类,通过 HostAppOptions 提供 15+ 个扩展回调点,允许在各阶段注入自定义逻辑。
应用场景
- 自定义中间件注册
- 自定义数据库配置
- 自定义服务注册
- 自定义 FreeSql 配置
HostApp 启动流程
1 2 3 4 5 6 7 8 9 10 11 12 13
| HostApp.Run(args, assembly) │ ├── 1. ConfigurePreWebApplicationBuilder // 前置 WebApplicationBuilder 配置 ├── 2. ConfigureWebApplicationBuilder // WebApplicationBuilder 配置 ├── 3. ConfigurePreServices // 前置服务注册 ├── 4. ConfigureServices // 核心服务注册 ├── 5. ConfigurePostServices // 后置服务注册 ├── 6. ConfigureMvcBuilder // MVC 构建器配置 ├── 7. ConfigureAutofacContainer // Autofac 容器配置 ├── 8. Build + ConfigurePreMiddleware // 前置中间件 ├── 9. ConfigureMiddleware // 中间件注册 ├── 10. ConfigurePostMiddleware // 后置中间件 └── 11. Run // 启动应用
|
启动项目
1 2 3
| var app = new HostApp(); app.Run(args, typeof(Program).Assembly);
|
HostAppOptions 扩展点
通过在启动代码中配置 HostAppOptions 实现自定义扩展。
服务注册阶段
| 回调 |
说明 |
ConfigurePreWebApplicationBuilder |
在创建 WebApplicationBuilder 之前配置 |
ConfigureWebApplicationBuilder |
配置 WebApplicationBuilder |
ConfigurePreServices |
前置服务注册 |
ConfigureServices |
核心服务注册阶段 |
ConfigurePostServices |
后置服务注册 |
ConfigureMvcBuilder |
配置 MVC 构建器 |
ConfigureAutofacContainer |
配置 Autofac DI 容器 |
中间件阶段
| 回调 |
说明 |
ConfigurePreMiddleware |
前置中间件注册 |
ConfigureMiddleware |
中间件注册 |
ConfigurePostMiddleware |
后置中间件注册 |
基础设施阶段
| 回调 |
说明 |
ConfigureFreeSqlBuilder |
配置 FreeSql 构建器 |
ConfigurePreFreeSql |
前置 FreeSql 配置 |
ConfigureFreeSql |
FreeSql 配置 |
ConfigureFreeSqlSyncStructure |
FreeSql 同步结构配置 |
ConfigureDynamicApi |
动态 API 配置 |
ConfigureSwaggerUI |
Swagger UI 配置 |
ConfigureIdGenerator |
雪花 Id 生成器配置 |
CustomInitDb |
自定义数据库初始化(跳过默认) |
使用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| var hostAppOptions = new HostAppOptions { ConfigurePreServices = context => { context.Services.AddSingletonimyservice,(); },
ConfigurePreMiddleware = context => { context.App.UseMyCustomMiddleware(); },
ConfigureFreeSql = (fsql, dbConfig) => { fsql.Aop.CommandBefore += (s, e) => { Console.WriteLine(e.Command.CommandText); }; },
CustomInitDb = true, };
var app = new HostApp(); app.Run(args, typeof(Program).Assembly, hostAppOptions);
|
AppInfo 全局访问
AppInfo 提供全局静态访问,在任意位置获取服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| var cache = AppInfo.GetServiceicachetool(); var config = AppInfo.GetRequiredServiceappconfig();
var appConfig = AppInfo.GetOptionsappconfig(); var dbConfig = AppInfo.GetOptionsdbconfig();
var userId = AppInfo.User?.Id;
var httpContext = AppInfo.HttpContext;
AppInfo.Log?.LogInformation("应用启动");
|
[!WARNING]
AppInfo 依赖应用启动后初始化完成,在 Program.cs 的早期阶段可能为 null。
ModuleInfo 模块信息
用于标识模块程序集和本地化类型。
1 2 3 4 5
| new ModuleInfo { Assembly = typeof(MyModuleService).Assembly, LocalizerType = typeof(MyModuleLocalizer) }
|
#中台 #中台/DI生命周期 #中台/数据库配置 #中台/新建接口项目