asp.net core: IApplicationLifetime.ApplicationStopping isn't triggered
Asked Answered
K

1

7

I saw a few articles about IApplicationLifetime and the way I can trigger execution when application starts and stops but I probably miss something because it is just not triggered. Here is my workaround: I opened a project template Container Application for kubernetes, asp.net core 2.2

Program.cs looks as it was created:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Kubernetes1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}

Startup.cs looks as following:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Kubernetes1
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //var appLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
            appLifetime.ApplicationStopping.Register(() => Console.WriteLine("ApplicationStopping called"));
            appLifetime.ApplicationStopped.Register(() => Console.WriteLine("ApplicationStopped called"));
            app.Run(async (context) =>
            {
                Console.WriteLine("AAA");
            });
        }
    }
}

All I wanted to do here is running the app in cmd, then stop it and see 2 lines printed in console:

  1. ApplicationStopping called
  2. ApplicationStopped called I didn't manage to make it happen. Any ideas?
Kwa answered 3/8, 2020 at 14:18 Comment(4)
How do you stop your program when check the output?Tassel
in a different cmd window I type: Get-Process -Name *dotnet* | Stop-ProcessKwa
Try cntrl+c in the same window.Tassel
Thanks! indeed it was the issue. did'nt know that stop-process isn't a gracefully stoppingKwa
P
4

in a different cmd window I type: Get-Process -Name *dotnet* | Stop-Process

Stop-Process will kill the process, skipping any graceful shutdown behavior an application might have.

When the IApplicationLifetime talks about the application stopping, then it refers to the application being gracefully shut down. There are a few ways to trigger this, depending on how the application is starting:

  • When the application is running in the console: CTRL + C. For example when running through dotnet run or running the compiled executable directly.
  • When the application is running in IIS: Stopping the website or the application pool.
  • When the application is running as a Windows Service: Stopping the service.
Purposely answered 3/8, 2020 at 15:10 Comment(2)
I am in a similar situation as above, but it won't work. I'm running in IIS Express. I use the task tray to "Stop Site", but my application stopping event is never hit. Does this not work with IIS Express perhaps?Pontius
I will had that in debug the "Stop debugging" doesn't trigger the graceful stop, you should follow the first case with CTRL + C in console.Flavouring

© 2022 - 2024 — McMap. All rights reserved.