--- phase: 01-gateway-routing plan: 03 type: execute wave: 2 depends_on: - 01-gateway-routing-01 - 01-gateway-routing-02 files_modified: - Fengling.Platform.Infrastructure/GatewayExtensions.cs autonomous: true requirements: - GATEWAY-04 - GATEWAY-05 must_haves: truths: - "Extensions 方法注册所有 Gateway 服务" - "DI 中 AddGatewayCore 后服务可用" - "可以生成数据库迁移" artifacts: - path: "Fengling.Platform.Infrastructure/GatewayExtensions.cs" provides: "AddGatewayCore 扩展方法" exports: ["GatewayDbContext", "IRouteStore", "IRouteManager", "IInstanceStore"] key_links: - from: "AddGatewayCore" to: "AddPlatformCore" via: "可以一起调用" pattern: "services.AddPlatformCore().AddGatewayCore()" --- # 计划 03: 网关扩展方法 ## 目标 创建 Extensions 类用于快速 IoC 注册 Gateway 服务。 **目的:** 支持单行注册所有 Gateway 服务,类似于 AddPlatformCore。 **输出:** 包含 AddGatewayCore 方法的 GatewayExtensions.cs。 ## 上下文 @Fengling.Platform.Infrastructure/Extensions.cs (模式参考) @Fengling.Platform.Infrastructure/GatewayDbContext.cs (来自计划 02) ## 任务 任务 1: 创建 GatewayExtensions Fengling.Platform.Infrastructure/GatewayExtensions.cs 创建 GatewayExtensions 静态类: ```csharp public static class GatewayExtensions { public static IServiceCollection AddGatewayCore(this IServiceCollection services) where TContext : GatewayDbContext { // 注册 Gateway stores services.AddScoped>(); services.AddScoped>(); // 注册 Gateway managers services.AddScoped(); return services; } } ``` 参考: Fengling.Platform.Infrastructure/Extensions.cs 模式 dotnet build Fengling.Platform.Infrastructure AddGatewayCore 扩展方法注册所有 Gateway 服务 任务 2: 生成数据库迁移 Fengling.Platform.Infrastructure/Migrations/ 为 Gateway 实体生成 EF Core 迁移: 1. 创建初始迁移: - 从 Infrastructure 目录 - 迁移名称: InitialGateway 2. 验证迁移包含: - GwTenants 表 - GwTenantRoutes 表 - GwServiceInstances 表 - GatewayDbContext 中定义的所有索引 dotnet ef migrations list --project Fengling.Platform.Infrastructure 迁移已生成并列出 ## 验证 - [ ] GatewayExtensions.cs 可编译 - [ ] AddGatewayCore 方法注册所有服务 - [ ] 迁移成功生成 - [ ] Build 通过 ## 成功标准 Gateway 路由功能完全集成并可使用。 ## 使用示例 ```csharp // 在 Program.cs 或 startup 中 services.AddPlatformCore(options => options.UseNpgsql(connectionString)); services.AddGatewayCore(options => options.UseNpgsql(gatewayConnectionString)); ```