Mike Phares 7be540964a Pull Request 33523 suggestions
Pull Request 33520 suggestions

Injected AppSettings instead of using GetEnvironmentVariable at Services level

When debugging only
app.Services.GetRequiredService<IPCRBService>();

Get ready to use VSCode IDE

Align .editorconfig files
2024-12-03 10:48:07 -07:00

42 lines
1.5 KiB
C#

using FabApprovalWorkerService.Models;
using Infineon.Monitoring.MonA;
namespace FabApprovalWorkerService.Services;
public class WindowsService : BackgroundService {
private readonly ILogger<WindowsService> _logger;
private readonly IMonInClient _monInClient;
public WindowsService(ILogger<WindowsService> logger,
IServiceProvider serviceProvider) {
_logger = logger ??
throw new ArgumentNullException("ILogger not injected");
using (IServiceScope scope = serviceProvider.CreateScope()) {
_monInClient = scope.ServiceProvider.GetService<IMonInClient>() ??
throw new ArgumentNullException("IMonInClient not injected");
}
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
_logger.LogInformation("Starting Windows service");
try {
while (!stoppingToken.IsCancellationRequested) {
_monInClient.PostStatus("WindowsService", State.Ok);
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
}
} catch (OperationCanceledException) {
_logger.LogError("The Windows service has been stopped");
_monInClient.PostStatus("WindowsService", State.Ok);
} catch (Exception ex) {
_logger.LogError($"An exception occurred when running Windows Service. Exception: {ex.Message}");
_monInClient.PostStatus("WindowsService", State.Critical);
Environment.Exit(1);
}
}
}