Graceful Shutdown not working anymore

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.

AFAIK IHostApplicationLifetime is still the recommended class to use for handling Shutdown event as per ASP.NET Core 5 docs:

Sorry I could not come back to this earlier.

I do not know what has changed, but my OnShutdown method is not called anymore. True is as well:

  • I develop on a mac
  • I use a virtualized Fedora Workstation to develop all of my server processes
  • All of them are deployed as Docker images
  • All of them run against .NETCore 3.1

However since Fedora 32, RedHat has integrated the entire .NET / .NETCore stuff in their package manager. So a sudo dnf update updates the .NET SDK, Runtime etc.
A quick check on my machine shows that I have 3.1 AND 5.0 on it:

[~] sudo dnf list installed | grep dotnet                                                                                                                         
dotnet.x86_64                                    5.0.203-1.fc32                      @updates              
dotnet-apphost-pack-3.1.x86_64                   3.1.15-1.fc32                       @updates              
dotnet-apphost-pack-5.0.x86_64                   5.0.6-1.fc32                        @updates              
dotnet-host.x86_64                               5.0.6-1.fc32                        @updates              
dotnet-hostfxr-3.1.x86_64                        3.1.15-1.fc32                       @updates              
dotnet-hostfxr-5.0.x86_64                        5.0.6-1.fc32                        @updates              
dotnet-runtime-3.1.x86_64                        3.1.15-1.fc32                       @updates              
dotnet-runtime-5.0.x86_64                        5.0.6-1.fc32                        @updates              
dotnet-sdk-3.1.x86_64                            3.1.115-1.fc32                      @updates              
dotnet-sdk-5.0.x86_64                            5.0.203-1.fc32                      @updates              
dotnet-targeting-pack-3.1.x86_64                 3.1.15-1.fc32                       @updates              
dotnet-targeting-pack-5.0.x86_64                 5.0.6-1.fc32                        @updates              
dotnet-templates-3.1.x86_64                      3.1.115-1.fc32                      @updates              
dotnet-templates-5.0.x86_64                      5.0.203-1.fc32                      @updates              

My docker images are built using a docker file which clearly includes the .NETCore 3.1 runtime.

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY publish .
.....

While researching I found an interesting Blog post explaining startup and shutdown in detail. To me it is clear that Windows Servers and UNIX Servers have different signalling. Since all my container run on RHEL I must use UNIX signalling. The question is now, does .NET detect this at runtime or do I have to change my Startup.cs file depending on the target runtime environment.

Looks like I have to investigate a bit more hereā€¦