1

I have this in my Startup.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Microsoft.Extensions.Hosting.IApplicationLifetime appLifetime)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseRouting();

    app.UseAuthorization();

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

    appLifetime.ApplicationStopping.Register(() => Console.WriteLine("ApplicationStopping called"));
    appLifetime.ApplicationStopped.Register(() => Console.WriteLine("ApplicationStopped called"));
}

I start my dotnet app: dotnet run --project myapp/myapp.csproj.

I get the process ID: ps | grep "dotnet run --project myapp/myapp.csproj"

I send the process a sigterm: kill <PID>.

The app terminates immediately and I don't see my console log messages. kill is supposed to send a SIGTERM by default.

Am I using IApplicationLifetime wrong or am I sending the SIGTERM wrong?

Edit

I think I might be using IApplicationLifetime wrong because I added this as well and I never see these messages either:

appLifetime.ApplicationStarted.Register(() => Console.WriteLine("ApplicationStarted called"));

halfer
  • 19,824
  • 17
  • 99
  • 186
red888
  • 27,709
  • 55
  • 204
  • 392
  • in Mac, are you running dotnet inside shell script, for instance, shell script calls `dotnet app.dll`. For us, adding `exec dotnet app.dll` helped to propagate SIGTERM signal to dotnet application, and not let shell script handle the SIGTERM directly. – AAATechGuy May 23 '22 at 22:39

1 Answers1

0

Documentation says "Triggered when the application host is performing a graceful shutdown." which does not looks like kill-ing for windows-born framework.

There are some old threads about how to trigger it manually: Getting ASP.Net Core shutdown triggering ApplicationStopping event in IISExpress also asp.net core: IApplicationLifetime.ApplicationStopping isn't triggered

But if you are really want to catch SIGTERM, you need to google "unix signal handlers".

There is a C API for this, there is also mono implementation, and finally there is (or will be soon) ms implementation.

Arthur Bulakaiev
  • 1,207
  • 8
  • 17
  • .net is supposed to do this ootb. k8s will send the process a sigterm when the pod is terminated. I need to mimic that locally for testing any logging/shutdown routines. – red888 Nov 12 '21 at 15:24