在仔細閱讀由Visual Studio 2013創建的AccountController代碼時,我看到了每次調用都執行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);
}
有一些優勢,我沒有看到這樣的await異步模式? 等待運營商使這些阻止調用,這使得他們基本上老式的同步調用。
回答
我還不能回答我的問題,由於缺乏聲譽,但我找到了答案here,而我沒有發佈之前搜索,我錯過了。這些調用是阻塞的,我錯過了,而且在文檔中根本不清楚,ASP.NET是在阻塞期間將當前工作線程返回到ASP.NET線程池。
進一步閱讀 TAP(基於任務的異步模式)是.NET Framework中異步的新模式。 Here鏈接到更多的模式信息比大多數人想要消化。
「等待操作員進行這些阻止呼叫」。這是導致你誤解的虛假陳述。 –
請詳細說明。在代碼繼續之前,使用上面的await操作符調用的方法必須完整,我會調用該阻塞。 – Mikee
我找到答案[這裏](http://stackoverflow.com/questions/19087513/what-is-the-advantage-of-using-async-with-mvc5),呼叫確實阻止。從MSDN幫助中不清楚的是,在阻止期間,當前的ASP.NET工作線程被釋放到池中,並且當異步調用完成時,重新獲取新的ASP.NET工作線程以繼續。 – Mikee