配置.NET Core通用主机时如何应用HostOptions.ShutdownTimeout? [英] How to apply HostOptions.ShutdownTimeout when configuring .NET Core Generic Host?

查看:324
本文介绍了配置.NET Core通用主机时如何应用HostOptions.ShutdownTimeout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET Core通用主机(不是Web主机)来构建需要相当长的正常关机时间的控制台应用程序。从源代码中

I am using the .NET Core Generic Host (not Web Host) to build a Console app that needs a rather lengthy graceful shutdown. From the source code in

aspnet / Hosting / src / Microsoft.Extensions.Hosting / HostOptions

显然, ShutdownTimeout 选项可用于更改作为参数提供的取消令牌中的关闭超时到 ShutdownAsync 。默认情况下,它是5秒。

it seems pretty clear that the ShutdownTimeout option can be used to change the shutdown timeout in the cancellation token that is provided as a parameter to ShutdownAsync. By default it is 5 seconds.

但是,我不知道在哪里以及如何编写代码来在 HostBuilder中指定此选项。 您通常放在 Program.cs 文件中的配置代码。

However, I can't figure out where and how to write the code to specify this option in the HostBuilder configuration code that you typically put in the Program.cs file.

有人可以发布一些代码来显示如何执行此操作?

Can someone post some code that shows how to do this?

推荐答案

好,我终于知道了...这是配置的概述我的Program.cs Main函数中的代码,其中大部分项目都被删除,以显示HostOptins.ShutdownTimeout的配置的去向。

OK, I finally figured it out ... Here's an outline the configuration code in my Program.cs Main function, with most of the items elided, to show where the configuration for HostOptins.ShutdownTimeout goes.

public static async Task Main(string[] args)
{
    var host = new HostBuilder()
        .ConfigureHostConfiguration(configHost => {...})
        .ConfigureAppConfiguration((hostContext, configApp) =>
           {...})
        .ConfigureServices((hostContext, services) =>
        {
           services.AddHostedService<ApplicationLifetime>();          
           ...
           services.AddOptions<HostOptions>().Configure(
                opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(10));
        })
        .ConfigureLogging(...)
        .UseConsoleLifetime()
        .Build();

    try
    {
        await host.RunAsync();
    }
    catch(OperationCanceledException)
    {
        ; // suppress
    }
}

要使此工作有效,这里是我的IHostedService类中的StopAsync方法:

To make the this work, here is the StopAsync method in my IHostedService class:

public async Task StopAsync(CancellationToken cancellationToken)
    {
try
        {
            await Task.Delay(Timeout.Infinite, cancellationToken);
        }
        catch(TaskCanceledException)
        {
            _logger.LogDebug("TaskCanceledException in StopAsync");
            // do not rethrow
        }
    }

请参见< a href = https://stackoverflow.com/questions/51044781/graceful-shutdown-with-generic-host-in-net-core-2-1>使用.NET Core 2.1中的通用主机进行正常关机

See Graceful shutdown with Generic Host in .NET Core 2.1 for more details about this.

Btw, Program.Main 中的catch块对于避免未处理是必要的即使我正在等待 StopAsync 中的取消令牌生成异常,也是如此;因为似乎在关闭超时到期时,由 StopAsync 的框架内部版本也会生成未处理的 OperationCanceledException

Btw, the catch block in Program.Main is necessary to avoid an unhandled exception, even though I am catching the exception generated by awaiting the cancellation token in StopAsync; because it seems that an unhandled OperationCanceledException is also generated at expiration of the shutdown timeout by the framework-internal version of StopAsync.

这篇关于配置.NET Core通用主机时如何应用HostOptions.ShutdownTimeout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆