Reputation: 1631
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.
Upvotes: 1
Views: 2166
Reputation: 1631
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.
Upvotes: -2
Reputation: 116518
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).
Upvotes: 6