8

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:

  • Installed Application Initialization feature on the Windows Server
  • Set "Preload" to true on the Web Site
  • On the AppPool, set "AlwaysRunning"
  • On the AppPool, set "Idle Timeout" to 0
  • On the AppPool, ensured it was running under "No Managed Code"
  • In the ApplicationInitialization configuration, set the following flags:
  • doAppInitAfterRestart = True
  • skipManagedModules = False
  • Added an initializationPage key, = "/start" (a blank MVC call just to start the app)

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?

1
  • Thanks for that question. I totally missed that the Application Initialization feature needs to be explicitly installed. Commented Jan 26, 2021 at 10:39

3 Answers 3

3

For the Set "Preload" to true on the Web Site to work you need also to activate anonymous authentication on the web site

Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

What exactly do you mean by "your app must be anonymous"?
3

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

4 Comments

This makes me want to cry. I've been running into this same error for months because of Hangfire, been pulling my hair out. Now I have to convince my boss to let me reinstall IIS on all our servers . . .
Ultimately, this is likely an IIS issue with conflicting global modules - I'm guessing that the installation process on prod servers is generally haphazard (install .NET core modules on IIS, then maybe WebSockets, then maybe other things, then App Initialization) - this would make it hard for MSFT to reproduce, which generally, they haven't been able to
I'm facing the exact issue with my local machine. Not sure what the conflicting global modules are...
Application initialization won't work with IISExpress (most extensions aren't built into it) - a full IIS installation on your local machine should work though, but reinstalling these packages on a desktop may cause more headache than it's worth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.