簡體   English   中英

調用可等待方法時是否真的需要 add.ConfigureAwait 調用?

[英]Is it really necessary to add .ConfigureAwait call when calling an awaitable method?

我正在使用帶有代碼驗證的 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 應用程序。

問候詹姆

真的有必要以這種方式弄臟代碼嗎?

如果您不阻止,則不需要ConfigureAwait(false)

在一般情況下, 建議使用ConfigureAwait(false)因為大多數代碼不知道其調用者是否會阻塞。 但是,如果此代碼是您的應用程序的一部分(即,不是庫),並且您確信您的應用程序不會阻塞異步代碼,那么您可以跳過ConfigureAwait(false)

Stephen Toub 最近發布了ConfigureAwait 常見問題解答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM