Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is IHostedService.StopAsync() called when IIS recycles?

This question is with regard to ASP.NET Core 2.2 applications running on IIS, specifically when registering instances of IHostedService with the DI container.

Every article I read about IHostedService.StopAsync() just talks about StopAsync() being called when the host "shuts down", but I want to know what happens when IIS recycles.

My question(s):

1. Is StopAsync() called when the IIS recycles?

2. Is the answer the same regardless of using InProcess hosting or OutOfProcess hosting? (ASP.NET Core supports InProcess as of version 2.2)

like image 609
Josh Withee Avatar asked Oct 21 '25 10:10

Josh Withee


1 Answers

  1. Is StopAsync() called when the IIS recycles?

Yes

Reference Implement background tasks in microservices with IHostedService and the BackgroundService class

Deployment considerations and takeaways

It is important to note that the way you deploy your ASP.NET Core WebHost or .NET Core Host might impact the final solution. For instance, if you deploy your WebHost on IIS or a regular Azure App Service, your host can be shut down because of app pool recycles.

.....

note: emphasis mine

which by extension would stop any IHostedService,

like image 76
Nkosi Avatar answered Oct 24 '25 08:10

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!