Refactored Worker to accept IConfiguration and read the delay interval from "Worker:DelayMilliseconds" in appsettings.json, replacing the previously hardcoded value. This allows the worker execution interval to be configured without code changes. Added a minimum delay safeguard and updated appsettings.json accordingly.
26 lines
795 B
C#
26 lines
795 B
C#
namespace EnvelopeGenerator.ServiceHost;
|
|
|
|
public class Worker : BackgroundService
|
|
{
|
|
private readonly ILogger<Worker> _logger;
|
|
private readonly int _delayMilliseconds;
|
|
|
|
public Worker(ILogger<Worker> logger, IConfiguration configuration)
|
|
{
|
|
_logger = logger;
|
|
_delayMilliseconds = Math.Max(1, configuration.GetValue("Worker:DelayMilliseconds", 1000));
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Information))
|
|
{
|
|
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
|
}
|
|
await Task.Delay(_delayMilliseconds, stoppingToken);
|
|
}
|
|
}
|
|
}
|