Recently I started using the IHostedService interface for some long running jobs in my API service. Of course there are many alternatives for setting up these scheduled jobs.

I followed the instructions at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio, tested locally and everything was running smoothly. My job was getting kicked off every 5 mins and I can see the output in my logs and debug output.

Problems started arising when I deployed my changes to my server (admittedly this API is running on a Windows 2012 R2 server, that is way past it’s life span). This is your typical “it works on my machine” issue. My background service would only launch once and never run again! Turns out IIS has a feature called Application Initialization that needs to be installed and then adjusted in two area:

Installation

Make sure to install Application Initialization 👇 Application Init

Application Pool configuration

Open up IIS manager and right click on the application pool, and then choose Advanced settings, set the start mode to AlwaysRunning

Application pool

Site configuration

And finally, right click on the website and select Manage Application -> Advanced settings, make sure to set Preload Enabled to True

Site configuration

Wrapping up

Make sure to perform an IISReset after making the changes, if you have any questions please feel free to contact me.