Reputation: 2390
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:
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.
Upvotes: 11
Views: 8614
Reputation: 1423
If you are looking to configure you app to start up immediately on a server reboot you can use these commands to set the AppPool and Application settings. This is useful when you can't remote desktop into the server. The below assume that the application is under the Default Web Site. You'll need to replace <APP POOL NAME>
and <APPLICATION NAME>
with the values for your scenario.
%windir%\system32\inetsrvappcmd.exe set apppool "<APP POOL NAME>" /startMode:AlwaysRunning
%windir%\system32\inetsrvappcmd.exe set app "Default Web Site/<APPLICATION NAME>" /preloadEnabled:true
Note: This was for a .NET 6 web application and assumes you already have the IIS Module Application Initialization already installed. If you don't have the IIS module installed then do that first before running the commands.
Upvotes: 0
Reputation: 2390
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.
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.
Upvotes: 14
Reputation: 4855
I tried the solution above by @eric, and it didn't work by itself. Adding this to my .CSPROJ file (in addition to his suggestions) finally did the trick:
<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
<ConcurrentGarbageCollection>false</ConcurrentGarbageCollection>
</PropertyGroup>
Upvotes: -1
Reputation: 5719
If you are using IIS there is a checkbox which starts the website immediately, you can try that on IIS settings.
UPDATE: see point 6 from below article:
https://learn.microsoft.com/en-us/aspnet/core/publishing/iis?tabs=aspnetcore2x
Upvotes: -1