Skip to content

UseCustomConsoleLifetime ​

Replaces the host's console lifetime so that Ctrl+C does not stop the application, while SIGTERM still triggers an orderly shutdown. Use it for a service that must not be killed by a stray key press in a shared terminal, but that still has to stop cleanly when an orchestrator asks it to. The replacement is unconditional, so ordering relative to Host.CreateApplicationBuilder does not matter.

Usage ​

csharp
using Microsoft.Extensions.Hosting;
using AlmightyShogun.Hosting.ConsoleLifetime;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

builder.UseCustomConsoleLifetime();
csharp
using Microsoft.Extensions.Hosting;
using AlmightyShogun.Hosting.ConsoleLifetime;

IHost host = Host.CreateDefaultBuilder(args)
    .UseCustomConsoleLifetime()
    .Build();
csharp
using AlmightyShogun.Hosting.ConsoleLifetime;
using Microsoft.Extensions.DependencyInjection;

builder.Services.UseCustomConsoleLifetime();

WARNING

There is no way to stop the application from the keyboard once this is registered outside an IDE. Stop it with docker stop, systemctl stop, or kill, all of which send SIGTERM, rather than Ctrl+C.

Shutdown behavior ​

SignalSourceBehavior
Ctrl+Cinteractive terminalSuppressed. The application keeps running.
Ctrl+CIDE run, with DOTNET_RUNNING_IN_IDE setAllowed, so the IDE stop button works.
SIGTERMdocker stop, Kubernetes, systemdOrderly shutdown through IHostApplicationLifetime.StopApplication. StopAsync runs on every hosted service, within ShutdownTimeout from ConfigureHostOptions, and registered services are disposed.

SIGTERM is honored on Linux and macOS. Windows has no SIGTERM, so only the Ctrl+C behavior applies there.

Returns

The same builder or service collection instance, with the custom IHostLifetime registered.

Type signature ​

csharp
public IServiceCollection UseCustomConsoleLifetime();

public IHostApplicationBuilder UseCustomConsoleLifetime();

public IHostBuilder UseCustomConsoleLifetime();

All packages are released under the MIT License.