Home > Enterprise >  .NET Core Worker Service as Windows service not stopping after calling StopAsync
.NET Core Worker Service as Windows service not stopping after calling StopAsync

Time:05-24

I have a worker service deployed as windows service and I am having problem stopping windows services, it shows stopped from the logs but when I logon to the server it still says running.

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

  public Worker(ILogger<Worker> logger)
  {
    this._logger = logger;
  }

  public override Task StartAsync(CancellationToken cancellationToken)
  {
    _logger.LogInformation("Windows Service is Starting....   ");
    return base.StartAsync(cancellationToken);
  }
  protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  {
    //initial value coming as false
    _logger.LogInformation(string.Format("Cancellation token initial value : {0}", stoppingToken.IsCancellationRequested));

        while (!stoppingToken.IsCancellationRequested)
        {
           try
           {
              _logger.LogInformation("Starting the Process .....");
               
              //Dummy task used here, so it can cancel token.
              await Task.Delay(1000);       
         
              //Deliberately failing in the string.Format to test windows service stopping
              string.Format("Failing with exception here : {1}", "Test");
            }
           
            catch (Exception ex)
            {
              _logger.LogError(ex, ex.Message);
               var canclSrc = new CancellationTokenSource();
               canclSrc.Cancel();
               await StopAsync(canclSrc.Token);
                   
               //Cancellation token here is coming as true..
               _logger.LogInformation(string.Format("Cancellation token value : {0}", stoppingToken.IsCancellationRequested));
               _logger.LogInformation("Windows Service is stopped..");
             }
         }
    //Windows service failed and it is logging outside the while loop.
    _logger.LogInformation("Came out of while loop as windows service stopped!");
  }

 public override Task StopAsync(CancellationToken cancellationToken)
 {
   _logger.LogInformation("Stopping Windows Service...");
   return base.StopAsync(cancellationToken);
 }

It is giving cancellation token as true in the logs, successfully calling the StopAsync method.

When I login into the server and see the windows service it is showing running and the windows service got struck somewhere I don't see any logs anything the service just hungs..

Any suggestion on why my windows service (which is a worker service) is not stopping on the server even when stopAsync is invoked.

CodePudding user response:

await StopAsync(canclSrc.Token);

Your background service shouldn't stop itself.

If you want to tear down your host (i.e., the Win32 Service), then you should call IHostApplicationLifetime.StopApplication (as described on my blog). Something like this:

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

  public Worker(ILogger<Worker> logger, IHostApplicationLifetime hostApplicationLifetime) => (_logger, _hostApplicationLifetime) = (logger, hostApplicationLifetime);

  protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  {
    _logger.LogInformation("Windows Service is Starting....   ");

    _logger.LogInformation(string.Format("Cancellation token initial value : {0}", stoppingToken.IsCancellationRequested));

    try
    {
      while (!stoppingToken.IsCancellationRequested)
      {
        _logger.LogInformation("Starting the Process .....");
               
        await Task.Delay(1000);       
         
        //Deliberately failing in the string.Format to test windows service stopping
        string.Format("Failing with exception here : {1}", "Test");
      }
    }
    catch (Exception ex)
    {
      _logger.LogError(ex, ex.Message);
    }
    finally
    {
      _logger.LogInformation("Windows Service is stopped..");
      _hostApplicationLifetime.StopApplication();
    }
  }
}
  • Related