SamJolly
SamJolly

Reputation: 6477

Async with MVC3 and ASP.NET4.5

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.

Upvotes: 2

Views: 610

Answers (2)

Koti Panga
Koti Panga

Reputation: 3720

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

Upvotes: 3

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12334

You can still use task parallel library

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

Upvotes: 0

Related Questions