简体   繁体   English

WebAPI控制器中的ConfigureAwait(false)

[英]ConfigureAwait(false) in WebAPI controller

Should SonarLint fire S3216 in an ASP.NET Web API controller? SonarLint应该在ASP.NET Web API控制器中触发S3216吗? It seems that this rule is for desktop applications, in ASP.NET the context is totally different, there's no danger of deadlocks. 看来这个规则适用于桌面应用程序,在ASP.NET中,上下文完全不同,没有死锁的危险。 Or am I missing something? 或者我错过了什么?

You should still use ConfigureAwait(false) in WebAPI when you don't need to capture the context. 当您不需要捕获上下文时,仍应在WebAPI中使用ConfigureAwait(false)

ConfigureAwait controls whether to resume on the captured SynchronizationContext or not. ConfigureAwait控制是否在捕获的SynchronizationContext上恢复。 It's true that this is a more painful issue in UI apps, but it's relevant everywhere there's a SynchronizationContext which is all UI apps and all asp.net apps. 确实,这在UI应用程序中是一个更痛苦的问题,但它与所有UI应用程序和所有asp.net应用程序的SynchronizationContext无关。

In UI apps the resource SynchronizationContext manages is the single UI thread so you can deadlock if you block it. 在UI应用程序中, SynchronizationContext管理的资源是单个UI线程,因此如果阻止它,您可能会死锁。 In asp.net apps the resource is the request context and you are able to deadlock on it as well. 在asp.net应用程序中,资源是请求上下文,您也可以对其进行死锁。

You can avoid using ConfigureAwait in console applications or windows services, though it's still a good practice to keep using it where appropriate. 您可以避免在控制台应用程序或Windows服务中使用ConfigureAwait ,但在适当的地方继续使用它仍然是一种很好的做法。

@VictorGrigoriu, we are only checking if the output kind of a compilation unit is a DLL or not, and we report issues only in DLLs. @VictorGrigoriu,我们只检查编译单元的输出类型是否是DLL,我们仅在DLL中报告问题。 You are right that we report on cases when in a DLL you still need to switch back to the original context. 你是对的,我们报告在DLL中你仍然需要切换回原始上下文的情况。 In general it's a very hard thing to figure out, but we could add a check for top level web app assemblies. 总的来说,要弄清楚这是一件非常困难的事情,但我们可以添加一个顶级Web应用程序集的检查。 We need to come up with a good way to do that or disable the rule by default to not generate false positives. 我们需要提出一个很好的方法来执行此操作或默认情况下禁用规则以不生成误报。

I've created a ticket to track this problem: https://jira.sonarsource.com/browse/SLVS-790 . 我创建了一张跟踪此问题的票证: https//jira.sonarsource.com/browse/SLVS-790

Other options until we come up with a permanent solution: you can disable this rule locally on the given project if you feel it's an annoyance. 其他选项,直到我们提出永久解决方案:如果您觉得这是一个烦恼,您可以在给定项目上本地禁用此规则。 To do that, you'll need to edit the project's ruleset file through "references/analyzers/open active rule set" 为此,您需要通过“references / analyzers / open active rule set”编辑项目的规则集文件

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

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