Jim*_*Jim 6 asp.net asp.net-mvc asynchronous asp.net-mvc-3
我有一个ASP.NET MVC3应用程序,可以处理耗时的进程(从网络复制大文件).我们想要做的是:
这个想法是用户不需要对过程的进度进行任何确认,也不会在过程完成时得到通知.
我们目前让控制器在Windows服务中触发事件,并使用Windows服务执行实际工作.我想知道是否有更好/更清洁的方法来做到这一点?
您可以使用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.Concurrency和IScheduler接口与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作为接口的实现.
归档时间: |
|
查看次数: |
1408 次 |
最近记录: |