【问题标题】:Is it really necessary to add .ConfigureAwait call when calling an awaitable method?调用可等待方法时是否真的需要添加 .ConfigureAwait 调用?
【发布时间】:2020-04-13 15:19:55
【问题描述】:

我正在使用带有代码验证功能的 Visual Studio 2019 开发应用程序。

一些代码验证提示很重要,但是,我在等待的方法调用中得到了很多提示,例如

var appUser = await UserManager.FindByIdAsync(User.Identity.GetUserId());

在这种情况下,系统建议我放

var appUser = await UserManager.FindByIdAsync(User.Identity.GetUserId()).ConfigureAwait(true);

var appUser = await UserManager.FindByIdAsync(User.Identity.GetUserId()).ConfigureAwait(false);

真的有必要这样弄脏代码吗?

这是一个 MVC5 ASP.NET 应用程序。

问候 詹姆

【问题讨论】:

    标签: async-await asp.net-mvc-5.2 configureawait


    【解决方案1】:

    真的有必要这样弄脏代码吗?

    如果你是do not block,那么你就不需要ConfigureAwait(false)

    在一般情况下,ConfigureAwait(false) is recommended 因为大多数代码知道它的调用者是否会阻塞。但是,如果此代码是您的应用程序的一部分(即不是库),并且您确信您的应用程序不会阻塞异步代码,那么您可以跳过 ConfigureAwait(false)

    Stephen Toub 最近发布了ConfigureAwait FAQ

    【讨论】:

      猜你喜欢
      • 2019-08-27
      • 2019-10-09
      • 1970-01-01
      • 2013-12-15
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多