0

I am looking at using QueueBackgroundWorkItem for background work items in my ASP.NET Web-API2 REST server. This works perfectly for my use case in that all jobs being run get completed and written to a DB before the web app is shut down. And the replacement web app takes over and can return the completed jobs.

However, when I complete a job in the background thread, I don't want to start another if IIS wants to shut the app down. Is there a way to find out if IIS is waiting on tasks to complete and will then be shutting down?

David Thielen
  • 28,723
  • 34
  • 119
  • 193

1 Answers1

0

You could try to use configure in startup class. below is configure parameters:

IApplicationBuilder

IHostingEnvironment

ILoggerFactory

IApplicationLifetime

In the IApplicationLifetime interface you can register an event that is triggered when the application stops:

public class Startup
{
    public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime)
    {
        applicationLifetime.ApplicationStopping.Register(OnShutdown);
    }

    private void OnShutdown()
    {
         //this code is called when the application stops
    }
}

you could refer this below links or more detail:

How to listen to IIS shutdown event in ASP.NET

Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26