I'm trying manually stop the class that inherit from BackgroundService
.
ExampleService.cs
public class ExampleService : BackgroundService, IExampleService
{
private readonly ILogger<ExampleService> _logger;
private bool stopRequested { get; set; }
public ExampleService(ILogger<ExampleService> logger)
{
_logger = logger;
}
public async Task Stoping(CancellationToken token)
{
_logger.LogInformation("Stoping");
stopRequested = true;
await StopAsync(token);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var count = 0;
while (!stoppingToken.IsCancellationRequested && !stopRequested)
{
await Task.Delay(1000, stoppingToken);
_logger.LogInformation("Service is working {0}", count++);
}
}
}
public interface IExampleService
{
Task Stoping(CancellationToken token = default);
}
Call from API
[HttpGet("stoptask2")]
public async Task<IActionResult> Get()
{
await exampleService.Stoping();
return Ok();
}
But the service doesn't stop. I know IHostApplicationLifetime
is working but all application is stopping. I don't want this.
I have tried How to cancel manually a BackgroundService in ASP.net core but it doesn't work.
I know I should stop the service using stoppingToken in ExecuteAsync, but how?