ThreadPool.QueueUserWorkItem in Web Service for “Fire and Forget” task

空扰寡人 提交于 2019-12-05 07:07:10

The problem is that every now and then, ASP.NET will recycle the app pool. Because it doesn't know about your background task, it will not be considered and will be aborted when the AppDomain is disposed.

Most of the time, the work will complete, but if you run for long enough, you will encounter this scenario.

There are two solutions:

1) The "proper" way is to write a Windows Service that runs outside ASP.NET. You can send instructions to the Service over WCF.

2) The "quick and dirty" way is to write a hidden web service in your ASP.NET site that is never called by users. Your app starts an asynchronous request to the hidden service and then returns its own result to the user, without waiting.

ASP.NET does not know that the request to the hidden service came from inside your app - it just treats it as another request. Because ASP.NET knows about this request, it will not abort it when it recycles.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!