C#C
C#2y ago
Kasumi

Windows Worker Service times out

Hello. Everytime I want to start my Worker Service I get the message that it doesnt reacted in time and I dont know why I get this error. because everytime I run the exe noremally the whole thing works.

Program.cs

using My_Service;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
IHost host = builder.Build();
await host.RunAsync();

Worker.cs
using System.Net.Mime;

namespace My_Service;


public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;

    public Worker(ILogger<Worker> logger)
    {
        _logger = logger;
    }
    
    
    
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("My Service running at: {time}", DateTimeOffset.Now);
            try
            {
                myServiceCode.systemcode();
                _logger.LogInformation("Started Service Service Code");
                //await Task.Delay(1_000, stoppingToken);
            }
            catch (Exception e)
            {
                _logger.LogError("Error in Service Code: {e}", e);
            }
            await Task.Delay(60000, stoppingToken);
        }
    }
}
Was this page helpful?