【问题标题】:What is the point of the CancellationToken in IHostedService.StartAsync in asp .net core?asp .net 核心中 IHostedService.StartAsync 中的 CancellationToken 有什么意义?
【发布时间】:2019-12-04 15:41:59
【问题描述】:

我正在尝试实现托管服务,但我想知道在 IHostedService.StartAsync 调用中我应该如何处理 CancellationToken。

Microsoft documentation 中有很多关于 StopAsync 中的取消令牌本质上是超时(默认为 5 秒),这意味着应该在合理的时间范围内发生正常关闭。

但是关于StartAsync中的token,信息不多。如果有的话,文档指定实现不应该等待长时间运行的初始化过程,而应该只返回一个长时间运行的任务。 因此,如果我支持取消,我是否应该将取消令牌传递给创建该长时间运行任务的任何对象?如果是,取消这个令牌不是更笨的StopAsync 版本吗?为什么框架会这样做?

事实上,在微软自己的抽象 BackgroundService 中,StartAsync 的实现实际上完全忽略了该令牌并创建了一个新令牌在调用 StopAsync 时被取消...

所以我认为框架传递的初始令牌的全部意义实际上是用于阻塞 通过覆盖虚拟BackgroundService.StartAsync 方法来初始化过程(尽管文档建议反对它)?例如

public class MyBackgroundService : BackgroundService
{
    public override Task StartAsync(CancellationToken cancellationToken)
    {
        // Block the thread for initialisation with cancellation (replace Task.Delay by actual initialisation)
        Task.Delay(TimeSpan.FromSeconds(10), cancellationToken).GetAwaiter().GetResult();

        // Start the long running task proper
        return base.StartAsync(cancellationToken);
    }

    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        // some long running process (write some actual background running code here)
        return Task.Factory.StartNew(_ => {while(!stoppingToken.IsCancellationRequested){}}, null, stoppingToken);
    }
}

【问题讨论】:

  • 这可能不是由于取消的特定用途。一般规则是每个异步方法都应该有一个取消令牌参数。因此,如果将来需要令牌,这不是重大更改。

标签: c# async-await asp.net-core-hosted-services


【解决方案1】:

传递给StartAsyncCancellationToken 可用于确定是否应该取消启动过程,但据我了解,您已经知道了。这是写here

表示启动过程已中止。

如果您查看where StartAsync is called,您可以看到传递给StartAsync 函数的CancellationTokenIHostApplicationLifetime.ApplicationStopping 链接。据我了解,如果应用程序关闭得太早以至于并非所有托管服务都已完全启动,则仅使用此令牌。但是,如果触发了起始令牌,我不知道是否会调用 StopAsync(我必须在源代码中进一步挖掘或在文档中搜索更多内容)。

总结一下:
StartAsync 方法中的 CancellationToken 应该只传递给支持取消的 StartAsync 中调用的任何方法。如果请求的关闭发生得太早以至于托管服务尚未完全启动,它将允许应用程序正常关闭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-11
    • 2019-06-29
    • 2018-10-17
    • 2020-02-18
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多