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

问题描述 投票:0回答:2

我有一个在.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
(这是来自框架),但是 movingToken 永远不会被设置(所以我无法关闭我的工作线程)。

stoppingToken 如何以及何时设置,以及如何优雅地终止我的工作人员?

控制台

c# .net-core background-service asp.net-core-hosted-services
2个回答
2
投票

对于后台服务,它们需要继续运行,直到取消令牌指示停止。为此,您需要一个 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");
    }

0
投票

你可以这样做:

await Task.Delay(TimeSpan.FromMinutes(10), stoppingToken);
© www.soinside.com 2019 - 2024. All rights reserved.