Home > Software engineering >  Does a Blazor server-side app actually start when its application pool gets recycled?
Does a Blazor server-side app actually start when its application pool gets recycled?

Time:04-04

I have a Blazor server-side app with a timed hosted service (implements IHostedService with a timer). The app is hosted in IIS and has its own application pool which gets recycled one a day.

I have noticed that after the pool gets recycled, I the timed hosted service doesn't start (I'm logging the events) even though the app is actually running. Once I access the app for the very first time, the service starts as expected.

Is this normal behavior for Blazor server-side apps?

CodePudding user response:

Mark Debono,

IIS will periodically recycle, similar to automatically restarting the website. The first visit after the website is started is often slow. After the website is started, there is no visit. After a period of time, IIS will be recycled. When the request comes again, it will be the same as when the website was just started. Need to wait for a while, this is caused by IIS recycling.

In addition to the above situation, the scheduled recycling will also recycle the hosted background tasks, causing the background tasks to terminate execution until a request (any address in the website) task starts, then if the request does not come for a long time, the task will not be able to was activated, missed the stage where the task should be executed. Although it is possible to adjust the time of automatic recycling, or even set it to not automatically recycle, this is not the best solution.

I think the following solution is better: preloading is achieved by configuring IIS, that is, when resources are recycled, wake up through a link, so that the first access will not feel stuck, and the scheduled tasks that are recycled It will restart again. The specific configuration method is as follows:

  1. Select the site and enable the preload option in the advanced settings. image1

  2. In the Configuration Editor, select system.webServer/applicationInitialization in section, Set the doAppInitAfterRestart option to true and add a collection item. image2

  3. Set the application pool startup mode to: AlwaysRunning image3

Of course, for the sake of insurance, you must pay attention to its operation when setting a scheduled task to avoid unnecessary trouble. It is best to record a log and pay attention to the operation of the website when it is idle and recycled at night to ensure stability. If your task still has Exception, then it is best to write a service to run scheduled tasks.

  • Related