using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Pgvector.EntityFrameworkCore; using RAG.Domain; using RAG.Domain.Interfaces; using RAG.Infrastructure.AI; using RAG.Infrastructure.Cache; using RAG.Infrastructure.Messaging; using RAG.Infrastructure.Persistence; using RAG.Infrastructure.Persistence.Interceptors; using Volo.Abp.Modularity; namespace RAG.Infrastructure; /// /// Infrastructure 层模块,注册数据库上下文、审计拦截器、Redis 缓存、RabbitMQ 消息队列。 /// 原 DependencyInjection.cs 的内容迁移至此。 /// [DependsOn(typeof(RAGDomainModule))] public class RAGInfrastructureModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { var services = context.Services; var config = services.GetConfiguration(); // DbContext 挂载审计拦截器,拦截器从 DI 容器解析 ICurrentUserContext services.AddDbContext((sp, options) => options.UseNpgsql(config.GetConnectionString("Default"), o => o.UseVector()) .AddInterceptors(sp.GetRequiredService())); // Scoped 生命周期:每个 HTTP 请求创建一个拦截器实例,确保 ICurrentUserContext 正确获取当前用户 services.AddScoped(); services.AddRedisCache(config); services.AddRabbitMq(config); // AI 服务注册 services.Configure(config.GetSection(AiOptions.SectionName)); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddSingleton(); } }