How to implement a very long running background task in Asp WebAPI application

人盡茶涼 提交于 2019-12-02 06:24:41

问题


I'm writing a WebAPI application to perform a number of long running background processing tasks.

I'm noticing, that my test data loading task, is being killed by System.Threading.ThreadAbortException, even after I disable the pool recycling in IIS. I know, that there is a way to run a very long running (a few hours long) background tasks within a Web application, but, I just do not know how exactly that is done - either some specific configuration settings, a coding technique, a Wen.config parameter, etc... Can anyone share some practical way of doing this, please?


回答1:


You DON'T want to use IIS/ASP.Net to run a long running process, it was not designed for that.

Only use it for the initial call in Web.API to kick off your service by using MSMQ or inserting a row into a table that tells your separate process what to do.

Create a separate application - either a Console application, where you would use Windows Scheduler to periodically run the app and check for new work items; or a Windows Service, that periodically checks your work item store for work items. You can then use the ThreadPool, etc to asynchronously run your process.




回答2:


It looks like the best answer to my problem is to combine a self-hosted WebAPI with TopShelf windows service implementation. This way I can write my WebAPI controller code the usual way, and those long running tasks will not get killed by IIS.

Thanks everyone for your help!

-Eugene



来源:https://stackoverflow.com/questions/21153329/how-to-implement-a-very-long-running-background-task-in-asp-webapi-application

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