Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a .Net Core website hosted in IIS to start immediately?

This blog post explains how when a .Net Core website is hosted in IIS, the website code does not actually get run until the first request comes in to IIS:

https://weblog.west-wind.com/posts/2016/Jun/06/Publishing-and-Running-ASPNET-Core-Applications-with-IIS

This is exactly the behavior that I am seeing, where once the first request comes in, everything starts running. I have a use case where I really need the website code to start as soon as IIS starts. Is there a way to programattically do this from within the website or to use something in configuration, so I do not have to remember to ping my website every time that IIS gets restarted? Thank you.

like image 711
Eric Avatar asked Oct 03 '17 19:10

Eric


1 Answers

I tried every auto start option I read about, and finally came up with the one combination that solved my problem. On the application pool set "Start Mode" to "AlwaysRunning" and on the website, itself, set "Preload Enabled" to "true". With those two settings, the application starts immediately. Now, I did discover something unexpected. When the website is in a stopped state, my application continues to run. What I found is that if I ever want to stop my application, I have to stop the application pool, not the website.

If this isn't working, also make sure that the Application Initialization feature is installed for IIS, since it is optional.

https://docs.microsoft.com/en-us/iis/get-started/whats-new-in-iis-8/iis-80-application-initialization

Core 3.1 Update: I have confirmed that this solution does work with Core 3.1. If you find that it is not working, double check that you have the Application Initialization feature installed for IIS. The downside to this solution is that stopping the app pool immediately stops my application, without a way to gracefully end. If the Application Initialization feature is not installed, my application will gracefully stop when the app pool is stopped, but then the application does not start automatically.

like image 197
Eric Avatar answered Oct 22 '22 21:10

Eric