ConfigureAwait(false) 与 ASP.NET Core 相关吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42053135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:52:39  来源:igfitidea点击:

ConfigureAwait(false) relevant in ASP.NET Core?

.netasp.net-coreasync-await

提问by Pedro Lorentz

I stumbled on an issue (https://github.com/HTBox/allReady/issues/1313) at GitHub where they discussed about taking the ConfigureAwait(false)out of the code, claiming that, in ASP.NET Core

我在 GitHub 上偶然发现了一个问题(https://github.com/HTBox/allReady/issues/1313),他们讨论了ConfigureAwait(false)ASP.NET Core 中删除代码的问题,声称

the call to ConfigureAwait(false)is redundant and does nothing

调用ConfigureAwait(false)是多余的,什么也不做

Best I could find here is a “side note” in an answer (from Stephen Cleary, https://stackoverflow.com/a/40220190/2805831) telling that

我能在这里找到的最好的答案是一个“旁注”(来自 Stephen Cleary,https://stackoverflow.com/a/40220190/2805831 )告诉我

ASP.NET Core no longer has a "context"

ASP.NET Core 不再有“上下文”

So, is ConfigureAwait(false)really unnecessary in ASP.NET Core(even if using full .Net Framework)? Does it have any real gain in performance in some cases or difference in the result/semantic?

那么,ConfigureAwait(false)ASP.NET Core 中真的没有必要吗(即使使用完整的 .Net Framework)?在某些情况下它是否有真正的性能提升或结果/语义的差异?

EDIT:Is it different in this aspect if I am hosting it as a console application or in IIS?

编辑:如果我将它作为控制台应用程序或在 IIS 中托管,在这方面是否有所不同?

回答by Paulo Morgado

ConfigureAwaitonly has effects on code running in the context of?a SynchronizationContextwhich?ASP.NET Core doesn't have (ASP.NET "Legacy" does).

ConfigureAwait只对在上下文中运行的代码有影响?SynchronizationContext哪个?ASP.NET Core 没有(ASP.NET“Legacy”有)。

General purpose code should still?use it because it might be running?with a SynchronizationContext.

通用代码仍然应该使用它,因为它可能正在运行?带有SynchronizationContext.

ASP.NET Core SynchronizationContext

ASP.NET Core 同步上下文

回答by Alfred Severo

What about this?

那这个呢?

At this moment (Feb-2020) Developers on MS Blog recommends use ConfigureAwait(false) in order to Improving performance, Avoiding deadlocks. https://devblogs.microsoft.com/dotnet/configureawait-faq/

目前(2020 年 2 月)MS 博客上的开发人员建议使用 ConfigureAwait(false) 以提高性能,避免死锁。 https://devblogs.microsoft.com/dotnet/configureawait-faq/

I've heard ConfigureAwait(false) is no longer necessary in .NET Core. True? False. It's needed when running on .NET Core for exactly the same reasons it's needed when running on .NET Framework. Nothing's changed in that regard.

我听说 .NET Core 中不再需要 ConfigureAwait(false)。真的?错误的。在 .NET Core 上运行时需要它,其原因与在 .NET Framework 上运行时需要的原因完全相同。在这方面没有任何改变。