| | | 1 | | using ProjectTemplate.Infrastructure.Data; |
| | | 2 | | using ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 3 | | using ProjectTemplate.Infrastructure.Data.Extensions; |
| | | 4 | | using ProjectTemplate.Infrastructure.Data.Services; |
| | | 5 | | using ProjectTemplate.Web.Accessors; |
| | | 6 | | using ProjectTemplate.Web.HealthChecks; |
| | | 7 | | using ProjectTemplate.Web.Options; |
| | | 8 | | |
| | | 9 | | namespace ProjectTemplate.Web.Extensions; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides service registration methods for application data access. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class DataAccessServiceExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Registers EF Core data access services for the web application. |
| | | 18 | | /// </summary> |
| | | 19 | | public static IServiceCollection AddApplicationDataAccess( |
| | | 20 | | this IServiceCollection services, |
| | | 21 | | IConfiguration configuration) |
| | | 22 | | { |
| | 139 | 23 | | ArgumentNullException.ThrowIfNull(services); |
| | 139 | 24 | | ArgumentNullException.ThrowIfNull(configuration); |
| | | 25 | | |
| | 139 | 26 | | services.AddHttpContextAccessor(); |
| | 139 | 27 | | services.AddScoped<ICurrentActorAccessor, HttpContextCurrentActorAccessor>(); |
| | 139 | 28 | | services.AddScoped<IApplicationAuditContextAccessor, HttpContextApplicationAuditContextAccessor>(); |
| | | 29 | | |
| | 139 | 30 | | services.AddApplicationInfrastructureDataAccess(configuration); |
| | 133 | 31 | | AddApplicationDatabaseReadinessCheck(services, configuration); |
| | | 32 | | |
| | 133 | 33 | | services.AddHostedService<DataAccessStartupLogger>(); |
| | | 34 | | |
| | 133 | 35 | | return services; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | private static void AddApplicationDatabaseReadinessCheck( |
| | | 39 | | IServiceCollection services, |
| | | 40 | | IConfiguration configuration) |
| | | 41 | | { |
| | | 42 | | // Bound lazily and read by the check on every run, so the setting follows the final configuration, including |
| | | 43 | | // sources added after service registration such as test or orchestrator overrides. |
| | 133 | 44 | | services |
| | 133 | 45 | | .AddOptions<ApplicationHealthCheckOptions>() |
| | 133 | 46 | | .Bind(configuration.GetSection(ApplicationHealthCheckOptions.SectionName)); |
| | | 47 | | |
| | | 48 | | // Infrastructure registers ApplicationDbContext only when a data access provider is enabled. |
| | 133 | 49 | | bool dataAccessEnabled = services.Any(descriptor => descriptor.ServiceType == typeof(ApplicationDbContext)); |
| | | 50 | | |
| | 133 | 51 | | if (!dataAccessEnabled) |
| | | 52 | | { |
| | 6 | 53 | | return; |
| | | 54 | | } |
| | | 55 | | |
| | 127 | 56 | | services.AddHealthChecks() |
| | 127 | 57 | | .AddCheck<ApplicationDatabaseHealthCheck>( |
| | 127 | 58 | | "application-database", |
| | 127 | 59 | | tags: [ApplicationHealthCheckTags.Ready, ApplicationHealthCheckTags.Database]); |
| | 127 | 60 | | } |
| | | 61 | | } |