1. Add IHostApplicationLifetime to your service
In modern ASP.NET Core applications, IHostApplicationLifetime is injected into services, such as controllers or services where you need to handle shutdown events.
In .NET 6 and 8, gracefully handling application shutdown is crucial for releasing resources or completing ongoing tasks before the application ends. The IApplicationLifetime interface was deprecated in favor of IHostApplicationLifetime in ASP.NET Core 3.0, and this approach continues in .NET 6/8.
In modern ASP.NET Core applications, IHostApplicationLifetime is injected into services, such as controllers or services where you need to handle shutdown events.