1

I have an ASP.NET Core 6 app running on IIS using the inProcess mode. In my Startup Class in the Configure method, I register for application lifetime events

hostApplicationLifetime.ApplicationStopping.Register(OnShutDown);
hostApplicationLifetime.ApplicationStarted.Register(OnStart);

I noted that my OnStart was firing twice: Once when I started the app pool on IIS, and once when I started the website. Further analysis showed that now only was OnStart getting called twice, but the whole Configure method was called twice. I'm using a singleton class that hosts some of my application services, and then OnStart is called the second time, the singleton is not there yet, so clearly we seem to be dealing with completely separate 'instances' of my application.

Any idea what's going on here?

To make matters even more interesting - when I stop the website, I'm also once again getting a call to Configure and OnStart. I then changed my app pool StartMode from AlwaysRunning to OnDemand. Now nothing happens if I start the app pool, and I'm getting a single call to Configure when I first access the website. But I'm still getting a call to Configure when I stop the website again. And I only get the OnShutdown when I stop the app pool.

Why is there separate instances for every app pool start when in AlwaysRunning mode and for every website start?

Stephan Steiner
  • 1,043
  • 1
  • 9
  • 22
  • https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.internal.applicationlifetime?view=dotnet-plat-ext-6.0#remarks "This API supports the .NET infrastructure and is not intended to be used directly from your code." – Lex Li Mar 18 '22 at 13:57
  • So, what's the alternative? This approach is the only thing I found to replace the lifecycle events that were present in Global.asap in ASP.NET And when it comes to that internal namespace, when even Andrew Lock suggest to use it (https://andrewlock.net/introducing-ihostlifetime-and-untangling-the-generic-host-startup-interactions/) I figure it should be okay to use.. – Stephan Steiner Mar 18 '22 at 14:18
  • Furthermore, this isn't about the `IHostApplicationLifeTime`. The problem is about App Pool Start creating a new hosted application (instantiate a new `Startup` Class, going through the startup motions with `Configure` and such). If that process sets up some things , every start/stop eats more and more memory until you run out of it. – Stephan Steiner Mar 18 '22 at 14:25
  • Could you please tell me how you between you use the IIS GUI to start the IIS website and start the application pool? – Brando Zhang Mar 20 '22 at 14:35
  • Start the IIS manager, go to app pools, right click, start/stop. And likewise for the website. I wouldn't know any other way to do that. – Stephan Steiner Mar 21 '22 at 07:58
  • I mean the time between using the IIS GUI to start the IIS website and start the application pool. – Brando Zhang Mar 23 '22 at 09:16
  • timing doesn't matter. I always first start the app pool. Then, maybe 30 seconds later (need to click around in iis manager to get to the proper website), I start the website => my app is restarted. – Stephan Steiner Mar 23 '22 at 13:18

0 Answers0