f513251507
Creato AssociationService per eliminare duplicazione codice e migliorare manutenibilità
Nuovo servizio:
- Data_Coupler/Services/AssociationService.cs (276 righe)
* Interfaccia IAssociationService con metodi pubblici
* PreDiscoveryRequest DTO per parametri configurabili
* FindOrCreateAssociationAsync(): ricerca locale + Pre-Discovery REST
* IsPreDiscoveryAssociation(): verifica marker associazioni Pre-Discovery
Refactoring DataCoupler.razor.cs:
- Injected IAssociationService nel componente
- StartDataTransferOriginal(): ridotto da 98 a 20 righe (-78)
- StartDataTransferWithComposite(): ridotto da 93 a 20 righe (-73)
- Verifica Pre-Discovery: ridotto da 20 a 2 righe (-18)
- Sostituito logica inline con chiamate al servizio centralizzato
Refactoring ScheduledProfileExecutionService.cs:
- Injected IAssociationService nel costruttore
- ExecuteDataTransferWithCompositeAsync(): ridotto da 99 a 20 righe (-79)
- Verifica Pre-Discovery: ridotto da 20 a 2 righe (-18)
- Parametro IsScheduledTransfer=true per tracciabilità
Dependency Injection:
- Registrato IAssociationService in Program.cs come Scoped
- Disponibile per dependency injection in tutti i componenti
Vantaggi:
- Eliminata duplicazione: 3 implementazioni → 1 servizio centralizzato
- Codice ridotto di 266 righe (330 → 64 nelle chiamate)
- Manutenibilità: modifiche future in un solo file
- Testabilità: interfaccia facilmente mockabile per unit test
- Riusabilità: servizio disponibile per futuri componenti
- Separazione responsabilità: logica associazioni isolata
Comportamento invariato:
- Nessuna modifica alla logica Pre-Discovery esistente
- Compatibilità completa con database e API
- Stessi marker e metadata nelle associazioni create
Docs: PRE_DISCOVERY_REFACTORING.md
Build: ✅ Successo (0 errori, 25 warning pre-esistenti)
204 lines
6.8 KiB
C#
204 lines
6.8 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
using DataConnection;
|
|
using DataConnection.EF;
|
|
using DataConnection.Interfaces;
|
|
using DataConnection.EF.DatabaseDiscovery;
|
|
using DataConnection.Enums;
|
|
using DataConnection.CredentialManagement;
|
|
using CredentialManager;
|
|
using Data_Coupler.Services;
|
|
using Data_Coupler.BackgroundServices;
|
|
using CredentialManager.Services;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
// Registra il provider di encoding per ExcelDataReader (necessario per file .xls)
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Configurazione per Windows Service
|
|
builder.Host.UseWindowsService(options =>
|
|
{
|
|
options.ServiceName = "DataCouplerService";
|
|
});
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddRazorPages();
|
|
builder.Services.AddServerSideBlazor();
|
|
builder.Services.AddWindowsService();
|
|
|
|
// Register Authentication Service
|
|
builder.Services.AddSingleton<Data_Coupler.Services.IAuthenticationService, Data_Coupler.Services.AuthenticationService>();
|
|
|
|
// Configurazione logging per Windows Service
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
builder.Logging.AddEventLog();
|
|
}
|
|
|
|
#region Database Directory Path management
|
|
|
|
string dbPath = string.Empty;
|
|
|
|
try
|
|
{
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
// Per servizi Windows, usa una cartella con permessi appropriati
|
|
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
|
|
if (string.IsNullOrEmpty(appDataPath))
|
|
{
|
|
// Fallback per servizi Windows
|
|
appDataPath = @"C:\ProgramData";
|
|
}
|
|
dbPath = Path.Combine(appDataPath, "Data_Coupler", "credentials.db");
|
|
}
|
|
else if (OperatingSystem.IsLinux())
|
|
{
|
|
dbPath = "/var/lib/Data_Coupler/credentials.db";
|
|
}
|
|
else if (OperatingSystem.IsMacOS())
|
|
{
|
|
dbPath = "/Library/Application Support/Data_Coupler/credentials.db";
|
|
}
|
|
|
|
var dbDirectory = Path.GetDirectoryName(dbPath);
|
|
if (!Directory.Exists(dbDirectory))
|
|
{
|
|
Directory.CreateDirectory(dbDirectory!);
|
|
|
|
// Per Windows, assicurati che la cartella abbia i permessi corretti
|
|
if (OperatingSystem.IsWindows() && !string.IsNullOrEmpty(dbDirectory))
|
|
{
|
|
var directoryInfo = new DirectoryInfo(dbDirectory);
|
|
// Imposta permessi per consentire l'accesso al servizio
|
|
}
|
|
}
|
|
|
|
Console.WriteLine($"Database path: {dbPath}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Errore nella configurazione del percorso database: {ex.Message}");
|
|
throw;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region OLD DB PATH
|
|
// // Add CredentialManager services
|
|
// var contentRoot = builder.Environment.ContentRootPath;
|
|
// var dataPath = Path.Combine(contentRoot, "wwwroot", "data");
|
|
// Directory.CreateDirectory(dataPath); // Assicurati che la directory esista
|
|
// var dbPath = Path.Combine(dataPath, "credentials.db");
|
|
#endregion
|
|
|
|
builder.Services.AddDataConnectionCredentialManagement($"Data Source={dbPath}");
|
|
|
|
// Register IHttpClientFactory
|
|
builder.Services.AddHttpClient();
|
|
|
|
// Register Data Connection Factory
|
|
builder.Services.AddScoped<IDataConnectionFactory, DataConnectionFactory>();
|
|
|
|
// Register Association Service (Pre-Discovery)
|
|
builder.Services.AddScoped<Data_Coupler.Services.IAssociationService, Data_Coupler.Services.AssociationService>();
|
|
|
|
// Register Backup Service
|
|
builder.Services.AddScoped<Data_Coupler.Services.IBackupService, Data_Coupler.Services.BackupService>();
|
|
|
|
// Register Schedule Services
|
|
builder.Services.AddScoped<IProfileScheduleService, ProfileScheduleService>();
|
|
|
|
// Register Data Transfer Service
|
|
builder.Services.AddScoped<Data_Coupler.Services.IDataTransferService, Data_Coupler.Services.DataTransferService>();
|
|
|
|
// Register Scheduled Profile Execution Service
|
|
builder.Services.AddScoped<Data_Coupler.Services.IScheduledProfileExecutionService, Data_Coupler.Services.ScheduledProfileExecutionService>();
|
|
|
|
// Register Background Services (solo uno per evitare duplicazioni)
|
|
builder.Services.AddHostedService<Data_Coupler.BackgroundServices.ScheduledJobService>();
|
|
|
|
// Configurazione URL e timeout per servizio Windows
|
|
var urls = builder.Configuration.GetValue<string>("Urls") ?? "http://*:7550";
|
|
builder.WebHost.UseUrls(urls);
|
|
|
|
// Configurazione timeout per servizio Windows
|
|
builder.WebHost.ConfigureKestrel(serverOptions =>
|
|
{
|
|
serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10);
|
|
serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(5);
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Initialize database con timeout e retry
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
|
|
try
|
|
{
|
|
logger.LogInformation("Inizializzazione database in corso... Path: {DbPath}", dbPath);
|
|
|
|
var dbInitializer = scope.ServiceProvider.GetRequiredService<CredentialManager.Services.IDatabaseInitializer>();
|
|
|
|
// Inizializzazione con timeout di 60 secondi
|
|
var initTask = dbInitializer.InitializeAsync();
|
|
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(60));
|
|
|
|
var completedTask = await Task.WhenAny(initTask, timeoutTask);
|
|
|
|
if (completedTask == timeoutTask)
|
|
{
|
|
logger.LogError("Timeout durante l'inizializzazione del database (60 secondi)");
|
|
throw new TimeoutException("Timeout durante l'inizializzazione del database");
|
|
}
|
|
|
|
await initTask; // Attendi il completamento per eventuali eccezioni
|
|
logger.LogInformation("Database inizializzato con successo.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Errore durante l'inizializzazione del database: {Message}. Path: {DbPath}", ex.Message, dbPath);
|
|
|
|
// Per servizi Windows, log su Event Log
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
try
|
|
{
|
|
using var eventLog = new System.Diagnostics.EventLog("Application");
|
|
eventLog.Source = "DataCouplerService";
|
|
eventLog.WriteEntry($"Errore inizializzazione database: {ex.Message}", System.Diagnostics.EventLogEntryType.Error);
|
|
}
|
|
catch
|
|
{
|
|
// Ignora errori di scrittura EventLog
|
|
}
|
|
}
|
|
|
|
throw; // Rilancia l'eccezione per non far partire l'app con un database non funzionante
|
|
}
|
|
}
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error");
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseStaticFiles();
|
|
|
|
app.UseRouting();
|
|
|
|
app.MapBlazorHub();
|
|
app.MapFallbackToPage("/_Host");
|
|
|
|
app.Run();
|
|
|