Prevent IIS from Idle Timeout when Running Background Tasks
Asked Answered
A

2

12

In our ASP.Net Core Web API, we have used Hosted Services to perform some background tasks. Sometimes it is possible that these background tasks to take a long time to complete (about an hour). Everything works seamlessly except when there are long-running tasks.

IIS Application Pool's default value for Idle Timeout is 20 mins. So when there is no request for 20 mins, IIS restarts the application pool. But sometimes it is possible that a background task running and IIS restarts irrespective of it causing tasks to terminate abruptly without marking it complete. As soon as IIS starts, tasks are picked up again by the hosted service start processing. But before completing, IIS restarts and this can go on forever. This can cause lots of issues when there are email sending tasks where same email can be potentially sent multiple times.

Since Hosted services are a first-class citizen of ASP.NET Core, It would be ideal if IIS considers background executions as well when performing idle timeouts.

Right now we have overcome this issue by setting Idle Timeout to 0 so that it never timeout. That's a kind of hacky solution. Also, it's not a viable option in load-balanced environments where nodes are adding and removing dynamically.

Is there a better way to "Prevent IIS from restarting while a background execution is in progress"?

Thanks

Audio answered 28/1, 2020 at 8:13 Comment(5)
Check this issuePartridgeberry
you could try some settings like Disable Idle timeouts(set to 0), Disable Regular time interval, set limit CPU interval to 0 check this image for reference: imageHuff
Thanks. That's what I have done right now. But that is not a proper solution.Audio
this the IIS setting to run set IIS application pool always running.Huff
I bump into this issue when running Hangfire server hosted in IIS Express (local environment)Arnst
O
15

Consider using Hangfire designed for such tasks. For the time being, You can configure your IIS to always run the site.

  1. Right-click site name > Manage WebSite > Advance > make sure preload is set to true.

  2. Right-click app pool > advance settings > Idle Time-out (minutes) to 0 and Start Mode to always running.

Oculo answered 28/1, 2020 at 13:56 Comment(3)
How to do this for IIS Express?Arnst
@Arnst typically, that question doesn't make a lot of sense due to the way IIS Express typically operates - that is, IIS Express's lifecycle is bound to a single app site - IIS Express is heavily optimized for single user/developer and debugging workflows.Fabianfabianism
It worked for me only after installing Application Initialization Module (IIS)Swig
F
2

I do not know your setup, but maybe you omit IIS and use something such as Kestrel.

Also, I would not put very long running tasks such as sending emails to run in an IIS process. Rather put the in another process such as a console app and leave the apsnetcore app to serve requests to the user.

Ferebee answered 28/1, 2020 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.