<p>I m trying to deploy a never ending message pump to process service bus queue messages as a .NET Core console app on Azure AKS Kubernetes.</p>

<p>The app will have auto-scaling based on the number of message in the queue, so more app may be deployed and when started they will connect to the service bus and start RegisterMessageHandler.</p>

<p>The auto-scaler may also teardown the app but without an event to signal the shutdown to the Console app I won't be able to close the queue properly to stop receiving messages or stop processing messages.</p>

<p>How does one handle that in .NET Core on AKS?</p>

fred_
<p>There are two signals available on Pod shutdown. The <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks" rel="nofollow noreferrer">PreStop hook</a> can be configured to trigger a shell command or generate an HTTP request against your container. You should also expect your running process to receive a TERM signal prior to the pod stopping. </p>

<p>Here's a <a href="https://blog.markvincze.com/graceful-termination-in-kubernetes-with-asp-net-core/" rel="nofollow noreferrer">blog post which covers some basics on hooking the TERM signal</a> by implementing <a href="https://github.com/dotnet/aspnetcore/blob/master/src/Hosting/Abstractions/src/IApplicationLifetime.cs" rel="nofollow noreferrer">IApplicationLifetime</a> within the context of a Kestrel server. </p>

<p>This article has a thorough end-to-end example of a <a href="https://dejanstojanovic.net/aspnet/2018/june/clean-service-stop-on-linux-with-net-core-21/" rel="nofollow noreferrer">simple implementation of IApplicationLifetime</a>.</p>

<p>For .NET 3.x, <a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostapplicationlifetime?view=dotnet-plat-ext-3.1" rel="nofollow noreferrer">IHostApplicationLifetime</a> supersedes IApplicationLifetime. </p>
