'Why does an application pool for ASP.NET Core in IIS take a long time to stop?

I developed a simple demo web app using ASP.NET Core 5 and published in IIS.

However, it takes a long time (over 30 seconds) when I press application pool stop button. Application pool cannot be started before it is stopped completed.

I have not experienced it with ASP.NET MVC 5 web app using .NET Framework 4.6.1 Application pool using ASP.NET MVC 5 can be stopped and started in a few seconds.


It may make issue with application pool idle time of IIS.

Of course, I can set application pool idle time to 0 to prevent auto shutdown of application pool.

However, I want that my web app is restarted automatically when it is idle for a long time like midnight, because my web app (not a simple demo web app but actual product) can consume very large memories.

If application restarting time is long, my web service can be down before stopping and starting is completed.

It is also important to update web app – ASP.NET Core 5 web app requires application pool shutdown to overwrite files.


If web service downtime is short (under 5 seconds), it can be acceptable to my customers.

However, if it is over 1 minite, it can be not good at real world scenario.


Is there any way to decrease application pool restarting time of ASP.NET Core 5 on IIS?


Solution 1:[1]

Possible reasons:

  • Using Thread.Sleep somewhere in application. When thread is sleeping, IIS waits for 30 sec (by default, can be configured in web.config). Thread.Sleep can be replaced by await Task.Delay (see more variants there)
  • Using HttpClient in some objects destructors
  • Some another work in objects destructors.

I investigated similar problem by sequently removing code from application and trying to stop pool.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pavel