80 lines
3.2 KiB
C#
80 lines
3.2 KiB
C#
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
|
|
static class CustomExtensionsMethods
|
|
{
|
|
public static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var hcBuilder = services.AddHealthChecks();
|
|
|
|
hcBuilder
|
|
.AddSqlServer(_ =>
|
|
configuration.GetRequiredConnectionString("OrderingDB"),
|
|
name: "OrderingDB-check",
|
|
tags: new string[] { "live", "ready" });
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddDbContexts(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
static void ConfigureSqlOptions(SqlServerDbContextOptionsBuilder sqlOptions)
|
|
{
|
|
sqlOptions.MigrationsAssembly(typeof(Program).Assembly.FullName);
|
|
|
|
// Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
|
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
|
};
|
|
|
|
services.AddDbContext<OrderingContext>(options =>
|
|
{
|
|
options.UseSqlServer(configuration.GetRequiredConnectionString("OrderingDB"), ConfigureSqlOptions);
|
|
});
|
|
|
|
services.AddDbContext<IntegrationEventLogContext>(options =>
|
|
{
|
|
options.UseSqlServer(configuration.GetRequiredConnectionString("OrderingDB"), ConfigureSqlOptions);
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddIntegrationServices(this IServiceCollection services)
|
|
{
|
|
services.AddTransient<IIdentityService, IdentityService>();
|
|
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
|
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
|
|
|
services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddApplicationOptions(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.Configure<OrderingSettings>(configuration);
|
|
services.Configure<ApiBehaviorOptions>(options =>
|
|
{
|
|
options.InvalidModelStateResponseFactory = context =>
|
|
{
|
|
var problemDetails = new ValidationProblemDetails(context.ModelState)
|
|
{
|
|
Instance = context.HttpContext.Request.Path,
|
|
Status = StatusCodes.Status400BadRequest,
|
|
Detail = "Please refer to the errors property for additional details."
|
|
};
|
|
|
|
return new BadRequestObjectResult(problemDetails)
|
|
{
|
|
ContentTypes = { "application/problem+json", "application/problem+xml" }
|
|
};
|
|
};
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
private static string GetRequiredConnectionString(this IConfiguration configuration, string name) =>
|
|
configuration.GetConnectionString(name) ?? throw new InvalidOperationException($"Configuration missing value for: {(configuration is IConfigurationSection s ? s.Path + ":ConnectionStrings:" + name : "ConnectionStrings:" + name)}");
|
|
}
|