I run my ServiceStack servers as Docker containers on Linux. I used to register a OnShutdown
callback to perform some cleanups. It looks like this callback does not work anymore.
I have the following in my Startup.cs
file:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
Logger.Information("Entering Startup.Configure()...");
var applicationLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
applicationLifetime.ApplicationStopping.Register(OnShutdown);
try
{
app.UseServiceStack(new AppHost("MyServer", typeof(SysOpService).Assembly)
{
AppSettings = new NetCoreAppSettings(Configuration)
});
}
catch (Exception e)
{
Logger.Error(e.ToString());
throw;
}
app.Run(context =>
{
context.Response.Redirect("/metadata");
return Task.FromResult(0);
});
}
private void OnShutdown()
{
Logger.Information("Shutting down server, performing some cleanup ...");
// ... perform cleanup tasks like removing cache and other stuff
}
However the OnShutdown
is never called. No matter whether I press Ctrl-C
in the IDE, a shell nor when I do a docker container stop MyServer
.
I did some research and found messages that IHostApplicationLifetime
seems to be outdated. Since all dotnetcore
applications are Console Applications
they should handle SIGTERM
or SIGKILL
events. I need to register a callback which gets fired when these events occur so I can gracefully shutdown and cleanup resources like removing cached entries, logout from other servers like message queue and other stuff.
Could anybody give me some pointers or an example how and where to implement this? What is the replacement of IHostApplicationLifetime
when running services as docker containers? Any help is greatly appreciated.