1

While perusing the AccountController code created by Visual Studio 2013. I see a pattern of sequential calls to async methods with each call performing await.

public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl){
  if (User.Identity.IsAuthenticated){
    return RedirectToAction("Manage");
  }

  if (ModelState.IsValid){
    // Get the information about the user from the external login provider
    var info = await AuthenticationManager.GetExternalLoginInfoAsync();
    if (info == null){
      return View("ExternalLoginFailure");
   }

   var user = new ApplicationUser() { UserName = model.UserName };
   var result = await UserManager.CreateAsync(user);
   if (result.Succeeded){
     result = await UserManager.AddLoginAsync(user.Id, info.Login);
     if (result.Succeeded){
       await SignInAsync(user, isPersistent: false);
       return RedirectToLocal(returnUrl);
      }
   }
   AddErrors(result);
 }

 ViewBag.ReturnUrl = returnUrl;
 return View(model);
}

Is there some advantage to this await-async pattern that I am not seeing? The await operators make these blocking calls which makes them basically old fashioned synchronous calls.

Answer

I can't yet answer my own question due to lack of reputation but I found the answer here, while I did search prior to posting, I missed it. The calls are blocking, what I missed, and is not at all clear in the documentation, is that ASP.NET is returning the current worker thread back to the ASP.NET thread pool during the blocking.

Further Reading TAP (Task based Asynchronous Pattern) is the new pattern asynchrony in the .NET Framework. Here is link to more info on the pattern than most will want to digest.

Community
  • 1
  • 1
Mikee
  • 1,571
  • 2
  • 14
  • 24
  • 6
    "The await operators make these blocking calls". That's the false statement driving your misunderstanding. – Matt Smith Jan 20 '14 at 16:17
  • 1
    Please elaborate. The methods called with the await operator above must be complete before the code continues, I would call that blocking. – Mikee Jan 20 '14 at 16:25
  • I found the answer [here](http://stackoverflow.com/questions/19087513/what-is-the-advantage-of-using-async-with-mvc5), the call does block. What is not clear from MSDN help is that during the blocking the current ASP.NET worker thread is released to the pool and when the async call completes a new ASP.NET worker thread is re-acquired to continue. – Mikee Jan 20 '14 at 16:43
  • @Mikee, async continuation is *not* a block, unlike say `Thread.Sleep`. Rather, the execution state of your code is saved into a compiler-generated object, and it simply stops executing, until some event happens. By analogy, when your close the lid of your laptop, and it goes into hibernate mode, is it a block? – noseratio Jan 20 '14 at 20:53

2 Answers2

6

Calling an async method does not block anything. The code, however, looks like it's synchronous. An async method returns a task that represents an asynchronous operation. The task ends when the operation ends.

What await does is it basically registers all the code after it as a continuation. It will run when the operation ends. It isn't blocked, it will be called when it needs to run. There's a big difference there.

For example, when I call a web service and print the result. Of course I can't print something I don't have, but instead of calling the service and waiting for the result I call the service and tell it what to do with the result (print).

i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • .....My usage of the term blocking was probably a bad choice as there is no thread blocked. If I now understand correctly, the await operator has the effect of not allowing the proceeding statement to execute until the 'await'ed method has completed. – Mikee Jan 20 '14 at 18:21
  • 1
    @Mikee And it's important to understand that control is passed back to the caller (i.e. the caller of ExternalLoginConfirmation continues to execute on the thread and is not blocked). – Matt Smith Jan 20 '14 at 18:58
  • @Mikee that's true, but the code after an await is described as a continuation. That makes it clearer to understand the asynchrony of it all. – i3arnon Jan 20 '14 at 21:58
  • The difference is usually best represented as; "returning-then awaiting" is when the UI keeps popping up little moving spinners briefly for the things that take time, while "blocking" is when the UI is always completely freezing up for half-seconds because the processor is not allowed to do anything else at all. – Katana314 Jan 20 '14 at 22:20
-2

The advantage to using await in the mentioned code is that ASP.NET worker thread is returned to the pool during the 'await'ed operation thus allowing for more scalability.

Document from Microsoft on the details from Microsoft on TAP (Task based Async Pattern). It is used to represent arbitrary asynchronous operations and per the document will be deprecating the Asynchronous Programming Model (APM) and the event-based asynchronous pattern (EAP) patterns of previous .NET Frameworks.

Mikee
  • 1,571
  • 2
  • 14
  • 24