5

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?
lior.mor
  • 158
  • 1
  • 9

1 Answers1

3

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.
poke
  • 369,085
  • 72
  • 557
  • 602
  • 1
    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? – Shane Oborn Dec 01 '20 at 19:51