Reputation: 2533
I've been following the IHostedService documentation in order to run a background task using the .NET Core MVC framework.
I would like the service to start automatically in IIS, however, it doesn't work.
I have done the following:
Oddly, these steps work on fresh Windows Server installation, but not on the server I want to use.
On the problematic server, I can see the w3wp process try to start the dotnet process (via Procmon), but it doesn't actually run any dotnet core code until I send a real HTTP request from a browser.
I have read many GitHub threads that say this should work regardless of the exact IIS version - what else could be going on?
Upvotes: 8
Views: 3862
Reputation: 2533
EDIT 2022: It appears the below still works but fails intermittently. The correct solution is as pointed out above - you also need to enable "Anonymous Authentication" in Authentication for the IIS site.
Windows auth can still be enabled side-by-side and .NET Core allows you to control authentication schemas after the program at least starts.
This appears to allow IIS COM processes to hit an unknown file that eventually passes through the account running the app pool - it would appear that AppInit operates out-of-proc for this initial request (so may be some local account or virtual temporary account).
I've found that the GC settings for .NET Core do not make a difference for this specific issue.
I went to extreme lengths for this one, including unregistering global modules, modifying DLLs manually, changing applicationHost.config, and blowing away all sorts of internal IIS configurations. None would get the application to start automatically.
I then took the nuclear approach: Reinstall IIS (reboots in between uninstall/reinstall)
This of course means losing some info about your existing hosted apps (such as binding info - if you see all your Sites are "Stopped" after reinstall, it's because there's no binding attached anymore), & after reinstalling I also had to:
Reinstall/Repair Web Deploy 3.5
Reinstall/Repair ASP.NET Core Hosting Bundles (For each affected version)
Reinstall/Repair .NET Core SDKs
Then everything started working as expected. Most of my IIS sites and settings stayed intact. I did not have to reinstall WAS (Activation Service) as appears to be commonly held in other posts.
Update: I've had scenarios where AppInit stops working on some servers even after temporarily resolving it by using the steps above - I've opened a bug for it here: https://github.com/dotnet/aspnetcore/issues/19509
Upvotes: 2
Reputation: 53
For anyone coming to this page in the future. I just got this working and what I finally figured out is that whatever endpoint you want to hit to "preload" your app must be Anonymous. Once I did this mine started working successfully.
Upvotes: 3
Reputation: 256
For the Set "Preload" to true on the Web Site to work you need also to activate anonymous authentication on the web site
Upvotes: 3