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

Jim*_*Jim 6 asp.net asp.net-mvc asynchronous asp.net-mvc-3

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

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

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

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

Ale*_*exC 6

您可以使用System.Threading.Tasks.Task使用Action委托调用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");
   }
}
Run Code Online (Sandbox Code Playgroud)

另一种选择是您可以使用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");
   }
}
Run Code Online (Sandbox Code Playgroud)

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