Async with MVC3 and ASP.NET4.5

。_饼干妹妹 提交于 2020-01-15 12:25:10

问题


I am using MVC3 and ASP.NET 4.5

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.

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.

ie.

    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();
    }

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

Many thanks in advance.


回答1:


MVC3 does not recognize Tasks.

You can get it from MVC4.

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.

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

Use below

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

Instead of

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



回答2:


You can still use task parallel library

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


来源:https://stackoverflow.com/questions/26935946/async-with-mvc3-and-asp-net4-5

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