InventoryAgent/Inventory.Agent/Program.cs
2025-10-20 00:03:49 +08:00

48 lines
2.1 KiB
C#

using Inventory.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Inventory.Agent
{
public class Program
{
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseWindowsService() // This needs to be configured for Windows Services
.ConfigureAppConfiguration((context, config) =>
{
// Setup secrets here, after the default configuration and environment are established.
// This ensures Secrets are populated before ConfigureServices is called.
EnvironmentBuilder.SetupEnvironment(context.HostingEnvironment);
})
.ConfigureServices((context, services) =>
{
var dbCon = Secrets.DbConnectionString;
if (string.IsNullOrWhiteSpace(dbCon))
{
// Use a logger or console write if appropriate, but throwing is safer for startup.
throw new InvalidOperationException("DB_CONNECTION_STRING is not configured. The application cannot start. Ensure it is in your .env file or Vault.");
}
services.AddDbContext<InventoryContext>(options => options.UseSqlServer(dbCon));
services.AddHttpClient();
services.AddSingleton<SystemInfoCollector>();
services.AddScoped<DatabaseUpdater>();
services.AddScoped<HealthMonitor>();
services.AddScoped<SlurpitClient>();
services.AddScoped<UpdateWorkflow>();
services.AddHostedService<Worker>();
});
}
}
}