Kestrel shutdown function in Startup.cs in ASP.NET Core

前端 未结 4 961
既然无缘
既然无缘 2020-11-29 07:07

Is there a shutdown function when using Microsoft.AspNet.Server.Kestrel? ASP.NET Core (formerly ASP.NET vNext) clearly has a Startup s

4条回答
  •  死守一世寂寞
    2020-11-29 07:46

    I solved it with the application lifetime callback events

    Startup.cs

    public void Configure(IHostApplicationLifetime appLifetime) {
     appLifetime.ApplicationStarted.Register(() => {
      Console.WriteLine("Press Ctrl+C to shut down.");
     });
    
     appLifetime.ApplicationStopped.Register(() => {
      Console.WriteLine("Terminating application...");
      System.Diagnostics.Process.GetCurrentProcess().Kill();
     });
    }
    

    Program.cs

    Also, use UseConsoleLifetime() while building the host.

    Host.CreateDefaultBuilder(args).UseConsoleLifetime(opts => opts.SuppressStatusMessages = true);
    

提交回复
热议问题