I have a problem with a self programmed windows service for .Net 6 that inherits from BackgroundService.
While other services I have programmed for .Net 6 run without problems, this service causes problems at startup.
The service reports an error 1053 and thinks that it does not respond within a time period. Sure, extensive initializations take place at startup, but they take a maximum of 2 seconds on my computer.
`
namespace WooComMesserschmidt
{
internal class Worker : BackgroundService
{
private readonly HttpClient Client = new()
private string BaseAddress = string.Empty;
private readonly IConfiguration Configuration;
private string ConfigFile = string.Empty;
private readonly Dictionary<string, dynamic?> _ConfigPar;
internal static Dictionary<string, string> ConfigPar = new();
private readonly ILogger<Worker> _logger;
public struct LogInfo
{
public ILogger<Worker>? Logger;
public string? LogFile;
public LogInfo(ILogger<Worker>? logger, string logfile)
{
Logger = logger;
LogFile = logfile;
}
}
public static LogInfo logInfo;
public Worker(ILogger<Worker> logger, IConfiguration configuration, Dictionary<string, dynamic?> configpar)
{
Configuration = configuration;
_ConfigPar = configpar;
_logger = logger;
Init();
}
...
`
In the method "Init()" relatively extensive tasks take place (on my PC it takes about 2 seconds). If these are through, it goes on here:
`
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (int.TryParse(ConfigPar[cpQueryInterval], out int QueryInterval) == false)
{
QueryInterval = QueryIntervalStd;
Log.LogInformation(logInfo, $"Abfrageintervall konnte nicht ermittelt werden. Es wird daher der Standardwert von {QueryInterval} Millisekunden verwendet. Prüfen Sie die Angaben in der Parameterdatei.");
}
Log.LogInformation(logInfo, $"{Process.GetCurrentProcess().ProcessName} gestartet.");
Log.LogInformation(logInfo, $"Worker arbeitet mit Abfrageintervall von {QueryInterval} Millisekunden.");
while (stoppingToken.IsCancellationRequested == false)
{
await ProcessNewOrders();
await UpdateProducts();
Dhl_Polling();
await Task.Delay(QueryInterval, stoppingToken);
}
}
...
`
The service is supposed to fetch orders from an eShop every few minutes, update items and process DHL shipments.
Well, when I start the program manually in the command line (i.e. not as a service), everything works as expected. Now I have registered the program as a service and every time I try to start the service I get the following error:
Error 1053: The service did not respond to the start or control request in a timely fashion
We started everything in Main:
`
private static async Task<int> Main(string[] args)
{
try
{
IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = ServiceName;
})
.ConfigureServices(services =>
{
services.AddSingleton<Dictionary<string, dynamic?>> (_ConfigPar);
services.AddHostedService<Worker>();
})
.Build();
await host.RunAsync();
Log.LogInformation((LogInfo)_ConfigPar[cpLogInfo], $"{Process.GetCurrentProcess().ProcessName} beendet.");
if (Debugger.IsAttached == true)
{
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex}{Environment.NewLine}{Environment.NewLine}");
...
`
How do I proceed to avoid this error?
Other services I have programmed, which are also based on .Net 6, work without any problems. However, with these the initialization is faster. But 2 seconds?
I have also entered the following in the registry:
Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
ServicesPipeTimeout (REG_DWORD): 0x180000
But this did not help.
Many thanks
René
Internet search, other forums and documentation