using Extensions.Hosting.AsyncInitialization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Tnsu.ApprovalRoute.Infrastructure.Persistence; using Tnsu.ApprovalRoute.Infrastructure.SharePoint; using Tnsu.ApprovalRoute.Infrastructure.TnsuPortal; namespace Tnsu.ApprovalRoute.Infrastructure; public static class InfrastructureServiceCollectionExtensions { public static IServiceCollection AddApprovalRouteInfrastructure( this IServiceCollection services, IConfiguration configuration) { services.AddHttpContextAccessor(); services.AddDbContext(opt => opt.UseNpgsql(configuration.GetConnectionString("Postgres")) .UseSnakeCaseNamingConvention()); services.AddAsyncInitializer(); services.Configure(configuration.GetSection("SharePoint")); // Провайдер сертификата — синглтон: разобранный .pfx кешируется на всё // время жизни процесса (см. DbCertificateProvider). services.AddSingleton(); services.AddHttpClient(); services.Configure(configuration.GetSection("TnsuPortal")); services.AddHttpClient(); // ITnsuPortalClient резолвится в CachingTnsuPortalClient (кеш на 5 минут, // см. её комментарий) — сам HttpTnsuPortalClient регистрируем по // конкретному типу, а не по интерфейсу, чтобы декоратор мог его получить // как обычную зависимость. services.AddHttpClient((sp, client) => { var options = sp.GetRequiredService>().Value; client.BaseAddress = new Uri(options.BaseUrl); }); services.AddMemoryCache(); services.AddScoped(); return services; } }