BackgroundService 未关闭,stoppingToken 从未使用 .net 核心通用主机设置

BackgroundService not shutting down, stoppingToken never set with .net core generic host

我在 .net 通用主机中托管了一个 BackgroundService,如下所示:

var builder = Host.CreateDefaultBuilder(args);

builder
    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.ClearProviders();
        logging.AddConsole();
        if(hostingContext.HostingEnvironment.IsDevelopment() == true)
            logging.AddDebug();
    })
    .ConfigureHostConfiguration(configurationBuilder =>
    {
        configurationBuilder.AddCommandLine(args);
    })
    .ConfigureAppConfiguration((hostingContext, configApp) =>
    {
        var env = hostingContext.HostingEnvironment;
        Console.WriteLine(env.EnvironmentName);
    })
    .UseConsoleLifetime();

然后我有我的工人:

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

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

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        // here i am doing something with ClientWebSocket, threads etc
        // snipped for brevity. I want to avoid spinning in a loop

        // the debugger reaches the following line
        WaitHandle.WaitAny(new[] { stoppingToken.WaitHandle });

        // but never hits this line
        this.logger.LogInformation("shutting down worker");
    }
}

在 运行 应用程序的 windows 终端按 ctrl+c 显示 Application is shutting down (这是来自框架)但是 stoppingToken 永远不会设置(所以我无法关闭我的工人)。

如何以及何时设置 stoppingToken,以及如何优雅地终止我的 worker?

控制台

对于 BackgroundServices,它们需要继续 运行 直到取消标记指示停止。为此,您需要一个 while 循环。但是,当您将此取消令牌传递给任何异步方法时,如果您在所有层中使用相同的令牌,它将从 运行 一直停止该方法通过链。它应该看起来像这样:

  protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            // here i am doing something with ClientWebSocket, threads etc
            // snipped for brevity. I want to avoid spinning in a loop
            ... 
            await client.DownloadAsync(..., stoppingToken);
            ...
        }

        // but never hits this line
        this.logger.LogInformation("shutting down worker");
    }