Вот MRE:
Код: Выделить всё
using Microsoft.Extensions.Hosting;
using System.Runtime.InteropServices;
ManualResetEvent resetEvent = new(false);
CancellationTokenSource cts = new();
// Handles CTRL + C in both operating systems
Console.CancelKeyPress += (sender, eventArgs) =>
{
Console.WriteLine("Received CTRL+C");
resetEvent.Set();
eventArgs.Cancel = true;
cts.Cancel();
};
// Handles graceful kill (Sigterm) in linux (e.g. systemctl stop)
PosixSignalRegistration.Create(PosixSignal.SIGTERM, (context) =>
{
Console.WriteLine("Received SIGTERM");
resetEvent.Set();
cts.Cancel();
});
IHost? host = null;
try
{
var hostBuilder = Host.CreateDefaultBuilder(args);
host = hostBuilder.Build();
host.Start();
resetEvent.Reset();
resetEvent.WaitOne();
}
catch
{
Console.WriteLine("Error");
}
finally
{
Console.WriteLine("finally");
await (host?.StopAsync(TimeSpan.FromSeconds(5)) ?? Task.CompletedTask);
Console.WriteLine("Application shut down");
}
Код: Выделить всё
Received SIGTERM
finally
info: Microsoft.Hosting.Lifetime[0]
Application is shutting down...
Application shut down
Код: Выделить всё
info: Microsoft.Hosting.Lifetime[0]
Application is shutting down...
Чего мне не хватает?
Подробнее здесь: https://stackoverflow.com/questions/744 ... lease-mode