How to perform a long-term task in ASP.NET 4?

I am building a website using .NET 4. There are many MSDN articles related to 2003 about using Thread and 2007 objects using asynchronous pages in .NET 2 , but it's all pretty old. I know that .NET 4 brought us a task class and some people vaguely warn about its use for this purpose .

So, I ask you, what is the “preferred” method around 2011 to run background / asynchronous work in IIS in ASP.NET 4? What caveats exist regarding the direct use of Thread / Task? Async = true still in fashion?

EDIT: Alright, alright, it's clear from the answers that I have to make a service if I can. But the advantages for this in webapp are important, especially simplify deployment / redistribution. Assuming the process is safe to crash, then if I did it in IIS, which is the best?

+8
asynchronous iis azure-webjobs
source share
5 answers

It is preferable to avoid performing long tasks in such an environment.

Delegate long-term tasks to a stable system service through interaction, leaving the web application responsive and required only for direct user requests.

Web applications have never been (and still are not considered) reliable systems - anyone who has ever used a browser has encountered (at least) a timeout to be sure; and such inconveniences (for both parties) are not limited to this scenario. Of course, any system can fail, but the circumstances associated with such an event in a system that must be stable must be completely exceptional.

Windows services are designed for a long time, and if something goes wrong, you tend to worry more about your personalized service.

+15
source share

Best avoided, but if you're forced to, think about Hanselmann’s thoughts on How to run background tasks in ASP.NET .

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

From personal experience, the task does not reduce it. QueueBackgroundWorkItem is much better.

+1
source share

My preferred method is the same as Robert Harvey suggests in his answer.

You can still use the parallel task library , but run the task in a separate process outside of IIS (the reason is that IIS has a limited number of workflows for distribution and imposes other restrictions that can make long-term tasks unpredictable).

0
source share

You can create a static ThreadPool similar to http://www.dotnetperls.com/threadpool with a limited number of threads (for example, only 2). and then queuing tasks, but this is highly discouraged because web servers are not for such tasks

0
source share

This is a once-daily scenario description.

If you really want to avoid creating a service, you can start the timer at intervals of 1 minute. Every time a timer delegate is called, you need to run something like this (pseudocode):

lastInvokeDay = LoadLastInvokeDate(); If (lastInvokeDay < DateTime.Now.Date && timeOfDayToRun == DateTime.Now.Time) { try { today = DateTime.Now.Date; runMyTask(); } catch.. finally { lastInvokeDay = today; SaveLastInvokeDay(lastInvokeDay); } } 

Keep in mind that lastInvokeDay must be stored either in the database or in a file ...

Now, if you want to enable immediate task invocation, you can simply call runMyTask() on demand. If it is important for you that runMyTask happens more than once a day, you could create a synchronized block of code inside it (with the lock instruction) and transfer the lastInvokeDay check inside.

Does this answer your question?

0
source share

All Articles