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

允我心安 提交于 2019-11-26 14:43:30

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

Justin Niessner

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

You can still use the Task Parallel Library, but spin the task up in a separate process outside of IIS (the reason being that IIS has a limited number of worker threads to hand out and imposes other limitations that can make long running tasks unpredictable).

This is a description of a 'once a day' scenario.

If you really want to avoid creating a service, you could start a timer with 1 minute intervals. Each time the timer delegate is invoked, you will have to run something like this (pseudo code):

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 the lastInvokeDay should be persisted either in Database or on a file...

Now, If you want to enable immediate invocation of the task, you could simply call runMyTask() on demand. If its important for you to keep the runMyTask from occuring more than once a day, you could create a syncronized block of code inside it (with a lock statement) and move the lastInvokeDay check inside.

Does this answer your question?

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