与MVC3和ASP.NET4.5异步 [英] Async with MVC3 and ASP.NET4.5

查看:81
本文介绍了与MVC3和ASP.NET4.5异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC3和ASP.NET 4.5

I am using MVC3 and ASP.NET 4.5

据我了解,即使我使用的是ASP.NET 4.5,也无法使用新的MS等待",任务"异步功能.我对么?我是否仍必须使用旧的"AsyncController"方法.

As I understand it, even though I am using ASP.NET 4.5, I cannot use the new MS "Await", "Task" Async features. Am I correct? Do I have to still use the old "AsyncController" approach.

最后,我要做的只是私有"一个私有方法,而不是完整的Action,就像"SendMail"方法调用会发生那样.

Finally, all I am trying to do is "Async" a private method, not the full Action, like what would happen with a "SendMail" method call.

即.

    public ActionResult Index()
    {

     SendMail(); // Inbuilt Async

     //Copy Document in DB. This is coded by private method in class, but needs to be fired asynchronously
     CopyDocument();

     ViewBag.Message = "Getting documents, Check back soon";


    return View();
    }

所以我如何仅同步"CopyDocument()".

So how can I async just "CopyDocument()".

非常感谢.

推荐答案

MVC3无法识别Tasks.

您可以从MVC4中获取它.

You can get it from MVC4.

更新: 尽管MVC3框架不完全完全支持Task async await,但是您可以使用@csevolodGoloviznin的答案中的Task.Factory库.

Update: Though MVC3 framework doesn't fully support Task async await directly you may use the Task.Factory library as you see in @VsevolodGoloviznin's answer.

在这种情况下,建议使用Task.Run,请参见 Stephen的博客,网址为startnew-is-dangerous 此博客了解更多信息

In this case recommendation would be use Task.Run see Stephen's blog on startnew-is-dangerous and this blog for more details

在下面使用

Task.Run(() => CopyDocument());

代替

Task.Factory.StartNew(() => CopyDocument());

这篇关于与MVC3和ASP.NET4.5异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆