【问题标题】:graceful shutdown asp.net core优雅关闭 asp.net 核心
【发布时间】:2020-02-13 18:06:45
【问题描述】:

遇到关于正常关闭asp.net核心应用程序的非常过时的信息,有人可以填写更新的信息吗?

用例:我想在应用程序退出时取消注册领事。

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((host, config) =>
        {
        })
        .UseStartup<Service>();

【问题讨论】:

    标签: asp.net-core graceful-degradation


    【解决方案1】:

    要捕获正常关机,您可以尝试IHostApplicationLifetime

    // Copyright (c) .NET Foundation. All rights reserved.
    // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
    
    using System.Threading;
    
    namespace Microsoft.Extensions.Hosting
    {
        /// <summary>
        /// Allows consumers to be notified of application lifetime events. This interface is not intended to be user-replaceable.
        /// </summary>
        public interface IHostApplicationLifetime
        {
            /// <summary>
            /// Triggered when the application host has fully started.
            /// </summary>
            CancellationToken ApplicationStarted { get; }
    
            /// <summary>
            /// Triggered when the application host is performing a graceful shutdown.
            /// Shutdown will block until this event completes.
            /// </summary>
            CancellationToken ApplicationStopping { get; }
    
            /// <summary>
            /// Triggered when the application host is performing a graceful shutdown.
            /// Shutdown will block until this event completes.
            /// </summary>
            CancellationToken ApplicationStopped { get; }
    
            /// <summary>
            /// Requests termination of the current application.
            /// </summary>
            void StopApplication();
        }
    }
    

    一个演示:

    public static void Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();
        var life = host.Services.GetRequiredService<IHostApplicationLifetime>();
        life.ApplicationStopped.Register(() => {
            Console.WriteLine("Application is shut down");
        });
        host.Run();
    }
    

    【讨论】:

    • 感谢您的输入,但是,我在这里找不到这个 IHostApplicationLifetime,我还注意到这个链接说它已经过时了,github.com/aspnet/Announcements/issues/344 请建议。
    • @AppDeveloper IApplicationLifetime 已过时,而不是 IHostApplicationLifetime。您的 .net 核心版本是什么?
    • 现在使用 .net 2.2
    • 你应该在 appLifetime.ApplicationStopped 或 ApplicationStopped 回调中做什么?我不明白这有什么帮助。假设你在控制器中有一个未知的当前正在进行的请求,它可以是 1、10 或 1000,你不知道。你希望他们都完成。这种方法有什么帮助?我希望有某种方式,说“当所有 HttpContext 都消失了,意思是,当没有更多的请求被处理时,请关闭”?
    • @Ted 这是优雅关机的目的;当应用程序关闭时,有一段时间(默认为 5 秒,但显然可以增加)来完成执行请求,之后应用程序被终止。 ApplicationStopping 在应用程序关闭启动时触发,ApplicationStopped 在优雅关闭完成时触发。如果 ApplicationStopped 被触发意味着 grecefull 关闭成功完成。
    猜你喜欢
    • 2021-03-31
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 2014-02-05
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多