处理ASP.NET MVC中的异步请求

我有一个ASP.NET MVC3应用程序,可以处理耗时的进程(从网络复制大文件).我们想要做的是:

>用户单击按钮以发布表单以触发该过程
>应用程序启动一个新线程以开始复制文件
>应用程序显示一条消息,指出文件复制过程已经开始
>用户可以在复制处理并在后台完成复制时关闭浏览器.

这个想法是用户不需要对过程的进度进行任何确认,也不会在过程完成时得到通知.

我们目前让控制器在Windows服务中触发事件,并使用Windows服务执行实际工作.我想知道是否有更好/更清洁的方法来做到这一点?

解决方法

您可以使用 System.Threading.Tasks.Task使用 Action delegate调用 StartNew方法.

使用这些工具你的控制器看起来像这样:

[HttpPost]
public ActionResult DoSomethingLongRunning()
{
   if (ModelState.IsValid)
   {
       Task.Factory.StartNew(() => 
                   filecopier.copyFile(copyFileParameter1,copyFileParameter2));

       return RedirectToAction("View Indicating Long Running Progress");
   }
   else
   {
        // there is something wrong with the Post,handle it
        return View("Post fallback view");
   }
}

一个选择是你可以使用System.Reactive.ConcurrencyIScheduler接口与TaskPoolScheduler作为执行操作的具体实现(可能在控制器构造函数中注入.

public ActionResult DoSomethingLongRunning()
{
   if (ModelState.IsValid)
   {
       ISchedulerImplementation.Schedule(new Action(() =>
        {
            filecopier.copyFile(copyFileParameter1,copyFileParameter2);
        }));
        return RedirectToAction("View Indicating Long Running Progress");
   }
   else
   {
        // there is something wrong with the Post,handle it
        return View("Post fallback view");
   }
}

作为一个好处,如果你这样做,你可以在单元测试时使用TestScheduler作为接口的实现.

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...