using FinlyticSentiment.Database; using FinlyticSentiment.Services; using FinlyticSentiment.Util; using Microsoft.EntityFrameworkCore; var builder = Host.CreateApplicationBuilder(args); // Register PostgreSQL DbContext for settings persistence builder.Services.AddDbContext(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); // Register HttpClient builder.Services.AddHttpClient(); // Register Service interfaces and co-located implementations builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); // Register MQTT Client (as singleton hosted service) builder.Services.AddSingleton(); builder.Services.AddHostedService(sp => sp.GetRequiredService()); // Register Sentiment Background Worker builder.Services.AddHostedService(); var host = builder.Build(); // Auto-migrate database on startup using (var scope = host.Services.CreateScope()) { try { var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); } catch (Exception ex) { var logger = scope.ServiceProvider.GetRequiredService>(); logger.LogError(ex, "An error occurred during database migration for FinlyticSentiment on startup."); } } await host.RunAsync();