
App Startup & Extensions
Namespace:
ZhonTai.Admin.Core
The entry point of Zhongtai Admin is the HostApp class. It provides 15+ extension callback points via HostAppOptions for injecting custom logic at various stages.
Startup Flow
HostApp.Run(args, assembly)
│
├── ConfigurePreWebApplicationBuilder // 1. Pre WebApplicationBuilder
├── ConfigureWebApplicationBuilder // 2. WebApplicationBuilder
├── ConfigurePreServices // 3. Pre service registration
├── ConfigureServices // 4. Core service registration
├── ConfigurePostServices // 5. Post service registration
├── ConfigureMvcBuilder // 6. MVC builder
├── ConfigureAutofacContainer // 7. Autofac container
├── Build + ConfigurePreMiddleware // 8. Pre middleware
├── ConfigureMiddleware // 9. Middleware
├── ConfigurePostMiddleware // 10. Post middleware
└── Run // 11. Start appStartup Project
csharp
// Program.cs
var app = new HostApp();
app.Run(args, typeof(Program).Assembly);HostAppOptions Extension Points
Service Registration Phase
| Callback | Description |
|---|---|
ConfigurePreWebApplicationBuilder | Before creating WebApplicationBuilder |
ConfigureWebApplicationBuilder | Configure WebApplicationBuilder |
ConfigurePreServices | Pre service registration |
ConfigureServices | Core service registration |
ConfigurePostServices | Post service registration |
ConfigureMvcBuilder | Configure MVC builder |
ConfigureAutofacContainer | Configure Autofac DI container |
Middleware Phase
| Callback | Description |
|---|---|
ConfigurePreMiddleware | Pre middleware registration |
ConfigureMiddleware | Middleware registration |
ConfigurePostMiddleware | Post middleware registration |
Infrastructure Phase
| Callback | Description |
|---|---|
ConfigureFreeSqlBuilder | Configure FreeSql builder |
ConfigurePreFreeSql | Pre FreeSql configuration |
ConfigureFreeSql | FreeSql configuration |
ConfigureFreeSqlSyncStructure | FreeSql sync structure |
ConfigureDynamicApi | Dynamic API configuration |
ConfigureSwaggerUI | Swagger UI configuration |
ConfigureIdGenerator | Snowflake ID generator |
CustomInitDb | Custom database initialization (skip default) |
Usage Example
csharp
var hostAppOptions = new HostAppOptions
{
// Custom service registration
ConfigurePreServices = context =>
{
context.Services.AddSingleton<IMyService, MyService>();
},
// Custom middleware
ConfigurePreMiddleware = context =>
{
context.App.UseMyCustomMiddleware();
},
// Custom FreeSql
ConfigureFreeSql = (fsql, dbConfig) =>
{
fsql.Aop.CommandBefore += (s, e) =>
{
Console.WriteLine(e.Command.CommandText);
};
},
// Custom DB initialization
CustomInitDb = true,
};
var app = new HostApp();
app.Run(args, typeof(Program).Assembly, hostAppOptions);AppInfo Global Access
AppInfo provides global static access to services from anywhere.
csharp
// Get service
var cache = AppInfo.GetService<ICacheTool>();
var config = AppInfo.GetRequiredService<AppConfig>();
// Get configuration
var appConfig = AppInfo.GetOptions<AppConfig>();
var dbConfig = AppInfo.GetOptions<DbConfig>();
// Current user
var userId = AppInfo.User?.Id;
// HttpContext
var httpContext = AppInfo.HttpContext;
// Logging
AppInfo.Log?.LogInformation("App started");WARNING
AppInfo depends on application startup completion. It may be null in early stages of Program.cs.