Josh Withee
Josh Withee

Reputation: 11336

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)

Upvotes: 4

Views: 1466

Answers (1)

Nkosi
Nkosi

Reputation: 247088

  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,

Upvotes: 2

Related Questions