ASP.NET Core 6 application lifetime on IIS - Startup.Configure firing multiple times

2k Views Asked by At

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?

0

There are 0 best solutions below