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

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


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.


It's best to be avoided, but if you are forced to, consider Hanselman's thoughts at How to run Background Tasks in ASP.NET.

Among them, and for something quick and easy, I would suggest you look in particular at the QueueBackgroundWorkItem added in 4.5.2.

From personal experience, Task does not cut it. QueueBackgroundWorkItem is much better.


You can create a static ThreadPool like this http://www.dotnetperls.com/threadpool with limited threads number(for example only 2). and then queue tasks in it, but it's highly not recommended because web servers are not for such kind of tasks