hrms-api-backend/BMA.EHR.Infrastructure/InfrastructureServiceRegistration.cs
Suphonchai Phoonsawat 18ab28e335
Some checks failed
release-dev / release-dev (push) Failing after 14s
fix: enhance database context configuration with retry logic for resilience
2025-12-15 11:12:29 +07:00

115 lines
4.6 KiB
C#

using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Repositories;
using BMA.EHR.Infrastructure.MessageQueue;
using BMA.EHR.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace BMA.EHR.Infrastructure
{
public static class InfrastructureServiceRegistration
{
public static IServiceCollection AddMessageQueue(this IServiceCollection services)
{
services.AddHostedService<RabbitMQConsumer>();
return services;
}
public static IServiceCollection AddLeavePersistence(this IServiceCollection services,
IConfiguration configuration)
{
// leave db context
var connectionStringLeave = configuration.GetConnectionString("LeaveConnection");
services.AddDbContext<LeaveDbContext>(options =>
options.UseMySql(connectionStringLeave, ServerVersion.AutoDetect(connectionStringLeave),
b =>
{
b.MigrationsAssembly(typeof(LeaveDbContext).Assembly.FullName);
b.MigrationsHistoryTable("__LeaveMigrationsHistory");
b.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: System.TimeSpan.FromSeconds(30),
errorNumbersToAdd: null);
}),
ServiceLifetime.Transient);
services.AddTransient<ILeaveDbContext>(provider => provider.GetService<LeaveDbContext>());
return services;
}
public static IServiceCollection AddDisciplinePersistence(this IServiceCollection services,
IConfiguration configuration)
{
// discipline db context
var connectionStringDiscipline = configuration.GetConnectionString("DisciplineConnection");
services.AddDbContext<DisciplineDbContext>(options =>
options.UseMySql(connectionStringDiscipline, ServerVersion.AutoDetect(connectionStringDiscipline),
b =>
{
b.MigrationsAssembly(typeof(DisciplineDbContext).Assembly.FullName);
b.MigrationsHistoryTable("__DisciplineMigrationsHistory");
b.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: System.TimeSpan.FromSeconds(30),
errorNumbersToAdd: null);
}),
ServiceLifetime.Transient);
services.AddTransient<IDisciplineDbContext>(provider => provider.GetService<DisciplineDbContext>());
return services;
}
public static IServiceCollection AddPersistence(this IServiceCollection services,
IConfiguration configuration)
{
services.AddTransient<MinIOService>();
var connectionString = configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<ApplicationDBContext>(options =>
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString),
b =>
{
b.MigrationsAssembly(typeof(ApplicationDBContext).Assembly.FullName);
b.MigrationsHistoryTable("__EHRMigrationsHistory");
b.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: System.TimeSpan.FromSeconds(30),
errorNumbersToAdd: null);
}),
ServiceLifetime.Transient);
services.AddTransient<IApplicationDBContext>(provider => provider.GetService<ApplicationDBContext>());
var connectionStringExam = configuration.GetConnectionString("ExamConnection");
services.AddDbContext<ApplicationDBExamContext>(options =>
options.UseMySql(connectionStringExam, ServerVersion.AutoDetect(connectionStringExam),
b =>
{
b.MigrationsAssembly(typeof(ApplicationDBExamContext).Assembly.FullName);
b.MigrationsHistoryTable("__EHRMigrationsHistory");
b.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: System.TimeSpan.FromSeconds(30),
errorNumbersToAdd: null);
}),
ServiceLifetime.Transient);
services.AddTransient<IApplicationDBExamContext>(provider => provider.GetService<ApplicationDBExamContext>());
return services;
}
}
}