简体   繁体   English

使用等待顺序调用异步任务的好处是什么?

[英]What Is The Advantage of Sequentially Calling Async Tasks Using Await?

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. 在仔细阅读由Visual Studio 2013创建的AccountController代码时。我看到一种顺序调用异步方法的模式,每次调用都在等待。

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. 这些调用是阻塞的,我错过了,并且在文档中根本不清楚,是在阻塞期间ASP.NET正在将当前工作线程返回到ASP.NET线程池。

Further Reading TAP (Task based Asynchronous Pattern) is the new pattern asynchrony in the .NET Framework. 进一步阅读 TAP(基于任务的异步模式)是.NET Framework中的新模式异步。 Here is link to more info on the pattern than most will want to digest. 是指向该模式的更多信息的链接,比大多数人想消化的更多。

Calling an async method does not block anything. 调用async方法不会阻止任何操作。 The code, however, looks like it's synchronous. 但是,代码看起来像是同步的。 An async method returns a task that represents an asynchronous operation. async方法返回代表异步操作的任务。 The task ends when the operation ends. 操作结束时任务结束。

What await does is it basically registers all the code after it as a continuation. await是它基本上将所有代码注册为连续代码。 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. 例如,当我调用Web服务并打印结果时。 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). 当然,我不能打印一些我没有的东西,但是我不调用服务并等待结果,而是调用服务并告诉它如何处理结果(打印)。

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. 在上述代码中使用await的好处是,ASP.NET辅助线程在“ await”操作期间返回到池中,因此可以实现更大的可伸缩性。

Document from Microsoft on the details from Microsoft on TAP (Task based Async Pattern). Microsoft提供的有关TAP(基于任务的异步模式)上Microsoft的详细信息的文档 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. 它用于表示任意异步操作,并且每篇文档将弃用以前.NET Framework的异步编程模型(APM)和基于事件的异步模式(EAP)模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM