Migrate legacy VB.NET service to modern C# Worker
Migrated the legacy VB.NET Windows Service to a modern C# Worker Service targeting .NET Framework 4.6.2. Key changes include: - Added `Microsoft.Extensions.Hosting.WindowsServices`, `NLog`, and `Quartz` for logging and scheduling. - Introduced `LogConfigFactory` to bridge `ServiceConfig` and `LogConfig` for Quartz jobs. - Rewrote `Worker` to handle the full service lifecycle, including database connection, temp file management, and scheduler initialization. - Implemented `SchedulerFinishEnvelope` and `SchedulerEnvelopeTaskApi` for Quartz job scheduling. - Created `TempFiles` for service-level temp file management. - Configured `NLog` for file and console logging. - Updated `appsettings.json` for connection strings and service-specific configuration. - Replaced legacy `ProjectInstaller` with `AddWindowsService()` for SCM integration.
This commit is contained in:
@@ -1,7 +1,41 @@
|
||||
using EnvelopeGenerator.Service;
|
||||
using EnvelopeGenerator.Service.Schedulers;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NLog.Extensions.Logging;
|
||||
|
||||
var builder = Host.CreateApplicationBuilder(args);
|
||||
|
||||
// NLog
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddNLog("nlog.config");
|
||||
|
||||
// Configuration
|
||||
builder.Services.Configure<ServiceConfig>(
|
||||
builder.Configuration.GetSection("ServiceConfig"));
|
||||
|
||||
// Resolve connection string once for singleton registrations
|
||||
var connectionString = builder.Configuration.GetConnectionString("Default")
|
||||
?? throw new InvalidOperationException("ConnectionStrings:Default is not configured.");
|
||||
|
||||
// Services
|
||||
builder.Services.AddSingleton<TempFiles>();
|
||||
|
||||
builder.Services.AddSingleton<SchedulerFinishEnvelope>(sp => new SchedulerFinishEnvelope(
|
||||
sp.GetRequiredService<ILogger<SchedulerFinishEnvelope>>(),
|
||||
sp.GetRequiredService<IOptions<ServiceConfig>>(),
|
||||
connectionString));
|
||||
|
||||
builder.Services.AddSingleton<SchedulerEnvelopeTaskApi>(sp => new SchedulerEnvelopeTaskApi(
|
||||
sp.GetRequiredService<ILogger<SchedulerEnvelopeTaskApi>>(),
|
||||
sp.GetRequiredService<IOptions<ServiceConfig>>(),
|
||||
connectionString));
|
||||
|
||||
builder.Services.AddHostedService<Worker>();
|
||||
|
||||
// Windows Service (SCM) support — replaces legacy ProjectInstaller/installutil
|
||||
builder.Services.AddWindowsService(options =>
|
||||
options.ServiceName = "EnvelopeGenerator.Service");
|
||||
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user