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 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); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); // 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"); builder.Services.AddDataConnectionCredentialManagement($"Data Source={dbPath}"); // Register IHttpClientFactory builder.Services.AddHttpClient(); // Register Data Connection Factory builder.Services.AddScoped(); var app = builder.Build(); // Initialize database using (var scope = app.Services.CreateScope()) { var logger = scope.ServiceProvider.GetRequiredService>(); try { logger.LogInformation("Inizializzazione database in corso..."); var dbInitializer = scope.ServiceProvider.GetRequiredService(); dbInitializer.InitializeAsync().GetAwaiter().GetResult(); logger.LogInformation("Database inizializzato con successo."); } catch (Exception ex) { logger.LogError(ex, "Errore durante l'inizializzazione del database: {Message}", ex.Message); 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();