The problem is that from time to time ASP.NET will process the application pool. Since he does not know about your background task, he will not be considered and will be interrupted when the AppDomain is deleted.
In most cases, the work is completed, but if you run long enough, you will come across this scenario.
There are two solutions:
1) The "correct" way is to write a Windows service that runs outside of ASP.NET. You can send instructions to the service through WCF.
2) The “quick and dirty” way is to write a hidden web service on your ASP.NET site that is never called by users. Your application launches an asynchronous request to a hidden service and then returns its own result to the user without waiting.
ASP.NET does not know that a request for a hidden service comes from your application - it just treats it as another request. Because ASP.NET is aware of this request, it will not interrupt it when it is reused.
source share