简体   繁体   中英

ASP.NET Core - Registering for Application Shutdown isn't working

I am registering for application shutdown but my method is not called (logs are not printed) when I stop the application.

Using new class: Microsoft.Extensions.Hosting.IHostApplicationLifetime

I am running and stopping my app in Visual Studio Code. My Startup.cs looks like this:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

    lifetime.ApplicationStopping.Register(OnShutdown);
    lifetime.ApplicationStopped.Register(OnShutdown);
}
protected void OnShutdown()
{
    Console.WriteLine("APPLICATION IS SHUTTING DOWN");
    Debug.WriteLine(">>Stopped");
}

If you're stopping the application by killing the debugger (ie pressing the "stop" button), then it's not going to call those. The program is dead the second you press it.

You would need to figure out a way to get SIGINT to the program. I don't know if that's possible or not in VS Code.

If you use IIS express you can do there by clicking 'Stop Site' or 'Stop All'. This also works for IIS express.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM