graceful shutdown asp.net core
Asked Answered
V

1

12

Coming across very outdated information on graceful shutdown of the asp.net core application, can someone fill in with the updated info.

Usecase: I'd like to unregister with consul on application exit.

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((host, config) =>
        {
        })
        .UseStartup<Service>();
Velazquez answered 16/10, 2019 at 21:49 Comment(0)
H
16

For capturing graceful shutdown, you could try 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();
    }
}

A demo:

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();
}
Hypesthesia answered 17/10, 2019 at 3:28 Comment(5)
Thanks for the input, however, I can't find this IHostApplicationLifetime in here, I also noticed this link that talks about it getting obsolete, github.com/aspnet/Announcements/issues/344 kindly advise.Velazquez
@Velazquez IApplicationLifetime is obsolete instead of IHostApplicationLifetime. What is your .net core version?Hypesthesia
using .net 2.2 nowVelazquez
What are u supposed to do in appLifetime.ApplicationStopping or ApplicationStopped callbacks? I dont see how that helps. Lets say u have an unknown currently ongoing requests in Controllers, it can be 1, 10 or 1000, you dont know. You want them all to finish. How does that metod help? I was hoping for some way, saying "When all HttpContext are gone, meaning, when there are no more requests being handled, shut down please"?Gilding
@Gilding this is the purpose of the graceful shut down; when the application is closed, there is an amount of time (5 seconds by default, but obviously it can be increased) to complete the executing requests, after that the application is terminated. ApplicationStopping is triggered when the application shut down start, ApplicationStopped is triggered when the gracefull shutdown is completed. If ApplicationStopped is triggered means the grecefull shutdown succesfully complete.Allure

© 2022 - 2024 — McMap. All rights reserved.