ASP.NET application (always running) shuts down application pool when publishing

5.4k views Asked by At

I've followed this guide here: Making ASP.NET application always running

However when I publish my web application to IIS 8.5 it shuts down the application pool and my event viewer shows the following message:

There was an error during processing of the managed application service 
auto-start for configuration path: 
'MACHINE/WEBROOT/APPHOST/mydomain.co.uk/'. The error message returned is: 
'An initialization error occurred while trying to preload an application.

Exception: System.InvalidOperationException

Message: Well known object of type 'System.Web.Hosting.PreloadHost' already 
exists in this App Domain.

StackTrace:    at 
System.Web.Hosting.HostingEnvironment.CreateWellKnownObjectInstance(String 
assemblyQualifiedName, Boolean failIfExists)
at 
System.Web.Hosting.HostingEnvironment.CreateWellKnownObjectInstance(String 
assemblyQualifiedName, Boolean failIfExists)
at System.Web.Hosting.ApplicationManager.CreateObjectInternal(String appId, 
Type type, IApplicationHost appHost, Boolean failIfExists, 
HostingEnvironmentParameters hostingParameters)
at System.Web.Hosting.ProcessHost.PreloadApplicationIfRequired(String appId, 
IApplicationHost appHostParameter, HostingEnvironmentParameters 
hostingParameters, LockableAppDomainContext ac)
at System.Web.Hosting.ProcessHost.<>c__DisplayClass38_0.
<PreloadApplicationIfNotShuttingdown>b__0(Object o)'.  The worker process will 
be marked unhealthy and be shutdown.  The data field contains the error code.

I can't for the life of me resolve this, or figure out where to start debugging it. If I manually start the application pool afterwards everything works fine - it's just an annoyance really. Any advice appreciated.

3

There are 3 answers

0
Captain_Planet On

Ahhhh. I've found the solution (well...it worked for me anyhow). And it was down to this great article: https://weblog.west-wind.com/posts/2013/oct/02/use-iis-application-initialization-for-keeping-aspnet-apps-alive

In particular, and what wasn't mentioned in the Hangfire tutorial above, was the need to:

  • Add the 'Applicaiton Initialization' role. On IIS 8.5 it was in a slightly different place to the linked article. It is in: Web Server (IIS) > Web Server > Application Development > Application Initialization

  • Also I needed to make sure that the 'Preload Enabled' flag was set, of which it wasn't. For that I highlighted my site in IIS, clicked 'Advanced Settings' in the right-hand panel, and set it there.

I then tried to publish my site again and voila....no HTTP Error 503!

UPDATE

This isn't the solution. The only reason it worked is because I'd attached the debugger (DebugDiag) and that seems to make it work for some reason! As soon as I detached the debugger I get the same error.

0
codeMonkey On

The selected answer helped me. If anyone else is having additional trouble, turned out I also didn't have the correct version of .NET installed on my machine; my app targeted 4.7, and the latest I had was 4.5. Once I got that installed, everything worked fine.

0
N30NF15H On

I increased the rapid fail protection maximum failures on the app pool to around 20. I'm not sure if this is the best way to solve it though.

  1. IIS Manager
  2. Application Pools
  3. Select app pool
  4. Advanced Settings
  5. Scroll down to Rapid-Fail Protection
    • Maximum Failures - I changed this to 20
  6. OK