изящное завершение работы ядра asp.net - PullRequest
0 голосов
/ 17 октября 2019

Перебирая очень устаревшую информацию о постепенном закрытии основного приложения asp.net, может кто-нибудь заполнить ее обновленной информацией.

Вариант использования: Я хотел бы отменить регистрацию в консуле при выходе из приложения.

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

1 Ответ

0 голосов
/ 17 октября 2019

Для получения плавного завершения вы можете попробовать 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();
}
...