问题
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