2025-08-14 13:12:16 -03:00
|
|
|
using Elecciones.Database;
|
2025-08-14 15:27:45 -03:00
|
|
|
using Elecciones.Infrastructure.Services;
|
2025-08-14 12:37:57 -03:00
|
|
|
using Elecciones.Worker;
|
2025-08-14 13:12:16 -03:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-08-15 17:31:51 -03:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Serilog;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Security;
|
|
|
|
|
using System.Security.Authentication;
|
|
|
|
|
|
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
.CreateBootstrapLogger();
|
|
|
|
|
|
|
|
|
|
Log.Information("Iniciando Elecciones.Worker Host...");
|
2025-08-14 12:37:57 -03:00
|
|
|
|
|
|
|
|
var builder = Host.CreateApplicationBuilder(args);
|
2025-08-14 13:12:16 -03:00
|
|
|
|
2025-08-15 17:31:51 -03:00
|
|
|
builder.Services.AddSerilog(config =>
|
|
|
|
|
config
|
|
|
|
|
.ReadFrom.Configuration(builder.Configuration)
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
.WriteTo.File("logs/worker-.log", rollingInterval: RollingInterval.Day));
|
2025-08-14 13:12:16 -03:00
|
|
|
|
2025-08-15 17:31:51 -03:00
|
|
|
// --- Configuración de Servicios ---
|
2025-08-14 13:12:16 -03:00
|
|
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
|
|
|
|
builder.Services.AddDbContext<EleccionesDbContext>(options =>
|
|
|
|
|
options.UseSqlServer(connectionString));
|
2025-08-14 15:27:45 -03:00
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
builder.Services.AddSingleton<IElectoralApiService, FakeElectoralApiService>();
|
|
|
|
|
#else
|
2025-08-15 17:31:51 -03:00
|
|
|
// --- SECCIÓN MODIFICADA (FINAL) ---
|
2025-08-14 15:27:45 -03:00
|
|
|
builder.Services.AddHttpClient("ElectoralApiClient", client =>
|
|
|
|
|
{
|
|
|
|
|
var baseUrl = builder.Configuration["ElectoralApi:BaseUrl"];
|
|
|
|
|
if (!string.IsNullOrEmpty(baseUrl))
|
|
|
|
|
{
|
|
|
|
|
client.BaseAddress = new Uri(baseUrl);
|
|
|
|
|
}
|
2025-08-15 17:31:51 -03:00
|
|
|
})
|
|
|
|
|
.ConfigurePrimaryHttpMessageHandler(() =>
|
|
|
|
|
{
|
|
|
|
|
return new SocketsHttpHandler
|
|
|
|
|
{
|
|
|
|
|
SslOptions = new SslClientAuthenticationOptions
|
|
|
|
|
{
|
|
|
|
|
// Forzamos el protocolo TLS 1.3
|
|
|
|
|
EnabledSslProtocols = SslProtocols.Tls13,
|
|
|
|
|
|
|
|
|
|
// --- ¡¡¡LA LÍNEA CLAVE CORREGIDA!!! ---
|
|
|
|
|
// Forzamos explícitamente los únicos 3 cipher suites que el servidor acepta.
|
|
|
|
|
CipherSuitesPolicy = new CipherSuitesPolicy(new[]
|
|
|
|
|
{
|
|
|
|
|
TlsCipherSuite.TLS_AES_128_GCM_SHA256,
|
|
|
|
|
TlsCipherSuite.TLS_AES_256_GCM_SHA384,
|
|
|
|
|
TlsCipherSuite.TLS_CHACHA20_POLY1305_SHA256
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-08-14 15:27:45 -03:00
|
|
|
});
|
2025-08-15 17:31:51 -03:00
|
|
|
|
2025-08-14 15:27:45 -03:00
|
|
|
builder.Services.AddSingleton<IElectoralApiService, ElectoralApiService>();
|
2025-08-15 17:31:51 -03:00
|
|
|
// --- FIN DE LA SECCIÓN MODIFICADA ---
|
2025-08-14 15:27:45 -03:00
|
|
|
#endif
|
|
|
|
|
|
2025-08-14 12:37:57 -03:00
|
|
|
builder.Services.AddHostedService<Worker>();
|
|
|
|
|
|
2025-08-14 15:27:45 -03:00
|
|
|
var host = builder.Build();
|
2025-08-15 17:31:51 -03:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
host.Run();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Fatal(ex, "El Host de Elecciones.Worker terminó inesperadamente");
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
}
|