5

I have an asp.net core application running on windows server. This is using some background tasks for using external service to get the data. We are facing an issue that after our app pool recycled/restarted, the background task is not running. This is running only after we are accessing the application. We found an article regarding this.

https://weblog.west-wind.com/posts/2013/Oct/02/Use-IIS-Application-Initialization-for-keeping-ASPNET-Apps-alive

Since this article describe asp.net application, below code will substitute the above solution in .net core application

public class Startup 
{
    public void Configure(IApplicationBuilder app) 
    {
        var applicationLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
        applicationLifetime.ApplicationStopping.Register(OnShutdown);
    }

    private void OnShutdown()
    {
     // Do your cleanup here
    }
}

Is there any other work around to solve this issue?

Shibu S R
  • 153
  • 1
  • 8
  • 1
    you can use hangfire to make tasks that can resume after recycle https://www.hangfire.io/ – Joe Audette Nov 30 '18 at 13:43
  • Is there no way to react on app pool recycling from within the Startup class (or any other place within my aspnet core application)? I need to have my background tasks started again in case the app pool is recycled. How can I achieve this? I don't want 3rd party software to achieve it. – thomasgalliker Jun 02 '19 at 11:15
  • The question includes the de facto way to react to it, though? ApplicationStopping will call the registered OnShutdown when recycling. Once pinged, the app will wire upp all services again. Rick's blog post that was linked includes a way to "keep alive", using an OnShutdown-http-ping to itself to start up immediately on recycle. Other ways include continuous external pinging, or controlling shutdown and/or recycling through "Application Pool" -> "Advanced Settings" -> "Idle Time-out Action" / "Recycling". – Classe Jun 13 '20 at 08:24

0 Answers0