本文介绍了如何使用HostBuilder通用主机ref StopAsync OperationCanceledException设置ShutdownTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看源代码,StopAsync的默认超时为5秒. WebHostBuilder 提供了扩展方法 UseShutdownTimeout 进行简单设置.但是 HostBuilder没有此类等效项.

Looking into the source code, the default Timeout for StopAsync is 5 seconds.WebHostBuilder provides an extension method UseShutdownTimeout for easy setting. But no such equivalent exists for HostBuilder.

我知道我可能想要超过5秒的超时来滥用HostBuilder的意图,但这是一个用于管理一系列相互依赖的作业的好框架.

I know I'm probably abusing the intent of HostBuilder by wanting more than 5 second timeout, but it's a nice framework for managing a collection of interdependent jobs.

我非常感谢有关如何使用HostBuilder进行操作的指南,以及UseShutdownTimeout对WebHostBuilder所做的操作,同时仍在使用正式的NuGet软件包.我看了一下扩展 HostingAbstractionsHostBuilderExtensions 但这是一个静态类...

I'd really appreciate some guidance on how to do with HostBuilder what UseShutdownTimeout does for WebHostBuilder whilst still working with official NuGet packages. I had a look at maybe extending HostingAbstractionsHostBuilderExtensions but it's a static class...

下面的示例控制台应用程序,用于触发StopAsync OperationCanceledException事件.在10秒钟之前,只需按Ctrl + C.

Sample console app below for triggering the StopAsync OperationCanceledException event. Simply Ctrl+C before 10 seconds are up.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace StackOverflow_GenericHost_StopAsync
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var host = new HostBuilder()                
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddSingleton<IHostedService, MustRunToCompletionService>();
                })

                .Build();


            await host.StartAsync();

            try
            {
                await host.WaitForShutdownAsync();
            }
            catch (OperationCanceledException ex)
            {
                // We have completed a controlled shutdown but the Exception is ugly
                Console.WriteLine(ex);
                Console.ReadKey();
            }

            // just hangs if we don't
            host.Dispose();

        }
    }

    class MustRunToCompletionService : IHostedService 
    {
        private Task _longRunningTask;

        private async Task MustCompleteProcess()
        {
            // simulate long running job
            Thread.Sleep(15000);
        }

        public Task StartAsync(CancellationToken cancellationToken)
        {
            _longRunningTask = Task.Run(MustCompleteProcess);
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            // ignore cancellationToken, I really need this to run to completion
            return Task.WhenAll(_longRunningTask);
        }

    }

}

推荐答案

我感谢这篇关于抑制状态消息

.ConfigureServices((hostContext, services) =>
    {
    services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(90));

这篇关于如何使用HostBuilder通用主机ref StopAsync OperationCanceledException设置ShutdownTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 00:59