using CellularManagement.Infrastructure.Context; using CellularManagement.Infrastructure.Options; using CellularManagement.Infrastructure.Repositories; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using CellularManagement.Domain.Entities; using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Abstractions; using CellularManagement.Application.Services; using CellularManagement.Infrastructure.Services; using System.Text; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using Microsoft.Extensions.Hosting; using Scrutor; namespace CellularManagement.Infrastructure; /// /// 依赖注入扩展方法 /// public static class DependencyInjection { /// /// 添加基础设施服务 /// /// 服务集合 /// 配置 /// 服务集合 public static IServiceCollection AddInfrastructure( this IServiceCollection services, IConfiguration configuration) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configuration); var assembly = typeof(DependencyInjection).Assembly; var databaseOptions = configuration .GetSection(DatabaseOptions.SectionName) .Get(); if (databaseOptions is null) { throw new ArgumentNullException( nameof(databaseOptions), "Database options not configured"); } // 配置 JWT 选项 services.Configure(configuration.GetSection(JwtOptions.SectionName)); services.ConfigureOptions(); // 配置数据库选项 services.Configure(configuration.GetSection(DatabaseOptions.SectionName)); var databaseOptionsConfig = configuration.GetSection(DatabaseOptions.SectionName).Get(); if (databaseOptionsConfig?.DefaultConnection is null) { throw new ArgumentNullException( nameof(databaseOptionsConfig.DefaultConnection), "Database connection string not configured"); } // 注册密钥轮换服务 services.AddSingleton(); services.AddHostedService(); // 配置数据库上下文 services.AddDbContext(options => { // 使用 PostgreSQL 数据库 options.UseNpgsql( databaseOptionsConfig.DefaultConnection, sqlOptions => { // 配置重试策略 sqlOptions.EnableRetryOnFailure(3); // 配置命令超时时间 sqlOptions.CommandTimeout(databaseOptionsConfig.CommandTimeout); }); // 配置详细错误信息 if (databaseOptionsConfig.EnableDetailedErrors) { options.EnableDetailedErrors(); } // 配置敏感数据日志记录 if (databaseOptionsConfig.EnableSensitiveDataLogging) { options.EnableSensitiveDataLogging(); } }); // 配置身份认证 services .AddAuthentication() .AddJwtBearer(); services .AddAuthorization(); // 配置身份认证服务 services .AddIdentityCore() .AddRoles() .AddEntityFrameworkStores(); // 注册工作单元 services.AddScoped(); // 注册仓储 services.AddScoped(typeof(ICommandRepository<>), typeof(CommandRepository<>)); services.AddScoped(typeof(IQueryRepository<>), typeof(QueryRepository<>)); // 添加内存缓存 services.AddMemoryCache(); // 注册缓存服务 services.AddScoped(); // 自动注册服务 services .Scan(action => { action .FromAssemblies(assembly) .AddClasses(false) .UsingRegistrationStrategy(RegistrationStrategy.Skip) .AsImplementedInterfaces() .WithScopedLifetime(); }); return services; } }