Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I perform a long-running task in ASP.NET 4?

I am building a website using .NET 4. There are lots of MSDN articles dating from 2003, about using Thread objects and 2007, using Asynchronous Pages in .NET 2, but that is all pretty stale. I know .NET 4 brought us the Task class and some people vaguely cautioning against its use for this purpose.

So I ask you, what is the "preferred" method circa 2011 for running background/asynchronous work under IIS in ASP.NET 4? What caveats are there about using Thread/Task directly? Is Async=true still in vogue?

EDIT: Ok, ok, from the answers it's clear the opinion is that I should make a service if I can. But the advantages to doing it inside the webapp are significant, especially easier deployment/redeployment. Assuming the process is safe-to-crash, then, if I were to do it inside IIS, what is the best way?

like image 752
Scott Stafford Avatar asked Apr 05 '11 13:04

Scott Stafford


1 Answers

Preferentially, avoid having long tasks executing in such an environment.

Delegate long running tasks out to a stable system service via interoperability, leaving the web application responsive and only required for direct user requests.

Web applications have never been (and still aren't) considered reliable systems - anyone who has ever used a browser has encountered (at least) a time-out, to be sure; and such inconvenience (for both parties) is not limited to this scenario. Of course, any system can crash, but the circumstances surrounding such an event on a system built-to-be-persistent ought to completely exceptional.

Windows services are designed to be long running, and if something goes wrong you've generally got more to worry about than your individual service.

like image 86
Grant Thomas Avatar answered Sep 16 '22 21:09

Grant Thomas