2

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?

user229044
  • 232,980
  • 40
  • 330
  • 338
Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167
  • This might help http://stackoverflow.com/questions/7913034/best-practice-longrunning-task-creation – Seano666 Jan 16 '14 at 04:29
  • Or maybe this http://stackoverflow.com/questions/19965342/launching-a-long-running-process-asynchronously/20650951#20650951 – sh1rts Jan 16 '14 at 04:30
  • In Azure, a cloud service (worker role) is the most robust mechanism. In WAWS, you can use the WebJobs SDK for long running asynch processes. See http://www.asp.net/aspnet/overview/developing-apps-with-windows-azure/getting-started-with-windows-azure-webjobs – RickAndMSFT Jan 23 '14 at 01:04

2 Answers2

2

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.

Cam Bruce
  • 5,632
  • 19
  • 34
  • 2
    With Azure WebJobs you now can add long running processes. See my tutorial http://www.asp.net/aspnet/overview/developing-apps-with-windows-azure/getting-started-with-windows-azure-webjobs. – RickAndMSFT Jan 21 '14 at 00:08
0

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

Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167