4

Created a new ASP.NET MVC project in Visual Studio 2013 (which is MVC 4 I guess?)

The default/template/generated code for the new project contains an Account controller. In that controller are async Task<ActionResult> Actions, but I don't really understand them, or why they are being used over syncrhonous code.

public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser() { UserName = model.UserName };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            await SignInAsync(user, isPersistent: false);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            AddErrors(result);
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

What is the benefit of making this call async? Why follow this pattern instead of making it a regular ActionResult method?

EMNova
  • 41
  • 1
  • The answers [here](http://stackoverflow.com/questions/19087513/what-is-the-advantage-of-using-async-with-mvc5) and [here](http://stackoverflow.com/questions/19573653/async-action-methods) might help –  Feb 18 '15 at 04:40

1 Answers1

0

The answer is more efficient resource use.

Generally, average Joe like you or I won't see any real difference. For fairly complex applications that are processing a lot of requests, the servers will thank you for appropriate use.

Asynchronous methods provide efficiency by not consuming threads when there is other work to be done (such as disk I/O). However, async has overhead; so there is no need to use it everywhere.

Use async:

  • You're calling services that can be consumed through asynchronous methods, and you're using .NET 4.5 or higher.
  • The operations are network-bound or I/O-bound instead of CPU-bound.
  • Parallelism is more important than simplicity of code.
  • You want to provide a mechanism that lets users cancel a long-running request.
  • When the benefit of switching threads out weights the cost of the context switch. In general, you should make a method asynchronous if the synchronous method blocks the ASP.NET request thread while doing no work. By making the call asynchronous, the ASP.NET request thread is not blocked doing no work while it waits for the web service request to complete.
  • Testing shows that the blocking operations are a bottleneck in site performance and that IIS can service more requests by using asynchronous methods for these blocking calls.

Don't use async:

  • The operations are simple or short-running.
  • Simplicity is more important than efficiency.
  • The operations are primarily CPU operations instead of operations that involve extensive disk or network overhead. Using asynchronous methods on CPU-bound operations provides no benefits and results in more overhead.

Source: Using Asynchronous Methods in ASP.NET 4.5

Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100
  • I guess what threw me was why this was the default for a new application. It seems that the decision to move something to async should be deliberate and with intent, because you know your operation will be I/O bound. Making it the default seems like overkill – EMNova Feb 19 '15 at 03:30