88 lines
5.4 KiB
C#
88 lines
5.4 KiB
C#
using Dapper;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NTP.Application.Abstractions;
|
|
using NTP.Application.Common;
|
|
using NTP.Application.Jobs;
|
|
using NTP.Infrastructure.HealthChecks;
|
|
using NTP.Infrastructure.Jobs;
|
|
using NTP.Infrastructure.Persistence;
|
|
|
|
namespace NTP.Infrastructure;
|
|
|
|
public static class DependencyInjection
|
|
{
|
|
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddOptions<OracleOptions>()
|
|
.Bind(configuration.GetSection(OracleOptions.SectionName))
|
|
.PostConfigure(o =>
|
|
{
|
|
// 1. ConnectionStrings:NAFITHTCS 2. ConnectionStrings:NTP (legacy) 3. Oracle:ConnectionString 4. Oracle:Host/Port/... parts
|
|
var named = configuration.GetConnectionString(OracleOptions.ConnectionStringName);
|
|
var legacy = configuration.GetConnectionString(OracleOptions.LegacyConnectionStringName);
|
|
if (!string.IsNullOrWhiteSpace(named))
|
|
o.ConnectionString = named;
|
|
else if (!string.IsNullOrWhiteSpace(legacy))
|
|
o.ConnectionString = legacy;
|
|
else if (string.IsNullOrWhiteSpace(o.ConnectionString) && o.HasParts)
|
|
o.ConnectionString = o.BuildFromParts();
|
|
})
|
|
.Validate(o => !string.IsNullOrWhiteSpace(o.ConnectionString),
|
|
"No Oracle connection configured. Set ConnectionStrings:NAFITHTCS, Oracle:ConnectionString, " +
|
|
"or Oracle:Host + ServiceName (or Sid) + UserId + Password in appsettings.json.")
|
|
.Validate(o => o.CommandTimeoutSeconds is > 0 and <= 600, "Oracle:CommandTimeoutSeconds must be 1-600.")
|
|
.ValidateOnStart();
|
|
|
|
services.AddOptions<NtpSchemaOptions>()
|
|
.PostConfigure(o => o.ConnectionString = configuration.GetConnectionString(NtpSchemaOptions.ConnectionStringName) ?? string.Empty)
|
|
.Validate(o => !string.IsNullOrWhiteSpace(o.ConnectionString), "No NTP-schema Oracle connection configured. Set ConnectionStrings:NtpSchema (user NTP).")
|
|
.ValidateOnStart();
|
|
|
|
// SHIPMENT_NO -> ShipmentNo, CREATED_AT -> CreatedAt (case-insensitive, underscores ignored).
|
|
DefaultTypeMap.MatchNamesWithUnderscores = true;
|
|
|
|
services.AddScoped<DbSession>();
|
|
services.AddScoped<IDbSession>(sp => sp.GetRequiredService<DbSession>());
|
|
services.AddScoped<IUnitOfWork>(sp => sp.GetRequiredService<DbSession>());
|
|
// Second connection (NTP schema). IDbSession / IUnitOfWork keep meaning the primary NAFITHTCS connection.
|
|
services.AddScoped<NtpDbSession>();
|
|
services.AddScoped<INtpDbSession>(sp => sp.GetRequiredService<NtpDbSession>());
|
|
services.AddScoped<INtpUnitOfWork>(sp => sp.GetRequiredService<NtpDbSession>());
|
|
services.AddScoped<IEventLogger, EventLogger>();
|
|
services.AddSingleton<ISystemErrorLogger, SystemErrorLogger>();
|
|
|
|
// scheduled jobs: the runner starts a job in its own scope, the worker looks for due jobs (screen "المهام المجدولة", SU only)
|
|
services.AddSingleton<IJobRunner, JobRunner>();
|
|
services.AddHostedService<JobSchedulerWorker>();
|
|
|
|
// mobile notifications run in the background in their own scope (see MobileNotificationGatewayService)
|
|
services.AddScoped<NTP.Infrastructure.Grains.NotificationDispatcher>();
|
|
|
|
// Refinery: Jopetrol claim web service (OFF unless Refinery:Jopetrol:Enabled; credentials only from env / .env)
|
|
services.AddOptions<NTP.Infrastructure.Integrations.JopetrolOptions>()
|
|
.Bind(configuration.GetSection(NTP.Infrastructure.Integrations.JopetrolOptions.SectionName));
|
|
services.AddHttpClient(NTP.Infrastructure.Integrations.JopetrolOptions.HttpClientName);
|
|
services.AddScoped<NTP.Application.Refinery.Integrations.IJopetrolGateway, NTP.Infrastructure.Integrations.JopetrolGateway>();
|
|
|
|
// Convention: every concrete *Repository / *Service in this assembly is registered against I{Name} (scoped).
|
|
// Do NOT add per-table lines here.
|
|
services.AddConventionalServices(typeof(DependencyInjection).Assembly);
|
|
|
|
// Refinery: Dinarak payment API (no "Service" suffix, so not convention registered). Off unless Refinery:Dinarak:Enabled.
|
|
services.AddOptions<NTP.Infrastructure.Integrations.DinarakOptions>()
|
|
.Bind(configuration.GetSection(NTP.Infrastructure.Integrations.DinarakOptions.SectionName));
|
|
services.AddHttpClient(NTP.Infrastructure.Integrations.DinarakOptions.HttpClientName, (sp, client) =>
|
|
client.Timeout = TimeSpan.FromSeconds(Math.Max(5, sp.GetRequiredService<Microsoft.Extensions.Options.IOptions<NTP.Infrastructure.Integrations.DinarakOptions>>().Value.TimeoutSeconds)));
|
|
services.AddScoped<NTP.Infrastructure.Integrations.DinarakGateway>();
|
|
services.AddScoped<NTP.Application.Refinery.Integrations.IDinarakGateway>(sp => sp.GetRequiredService<NTP.Infrastructure.Integrations.DinarakGateway>());
|
|
services.AddScoped<NTP.Application.Refinery.Integrations.IDinarakReferenceGateway>(sp => sp.GetRequiredService<NTP.Infrastructure.Integrations.DinarakGateway>());
|
|
|
|
services.AddHealthChecks()
|
|
.AddCheck<OracleHealthCheck>("oracle", tags: ["ready"])
|
|
.AddCheck<NtpSchemaHealthCheck>("oracle-ntp", tags: ["ready"]);
|
|
|
|
return services;
|
|
}
|
|
}
|