Rule: CA2007 & MA0004 Do not directly await a Task
davidkallesen opened this issue · 6 comments
Rule summary
Value | |
---|---|
Title | CA2007 Do not directly await a Task |
CheckId | CA2007 |
Category | Microsoft.Reliability |
Link | https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2007 |
What is the problem
I work primarily with projects that do not have a SynchronizationContext
, therefore this rule is not usable I think.
And very annoying to have to type .ConfigureAwait(false)
everywhere if not needed.
But am not quite sure after reading these 2 blog post:
- https://blog.stephencleary.com/2017/03/aspnetcore-synchronization-context.html
- https://devblogs.microsoft.com/dotnet/configureawait-faq/#ive-heard-configureawaitfalse-is-no-longer-necessary-in-net-core-true
But most certainly, this rule does not make sense at all in test projects.
Note: this rule is the same same as MA0004
Suggestion
I am not sure, but think it should be removed/suppressed from test
projects at least.
We can have our cake and eat it to. This can be enabled on a per project type, per MS's docs:
You can configure whether you want to exclude asynchronous methods that don't return a value from this rule. To exclude these kinds of methods, add the following key-value pair to an .editorconfig file in your project:
# Package version 2.9.0 and later
dotnet_code_quality.CA2007.exclude_async_void_methods = true
# Package version 2.6.3 and earlier
dotnet_code_quality.CA2007.skip_async_void_methods = true
You can also configure which output assembly kinds to apply this rule to. For example, to only apply this rule to code that produces a console application or a dynamically linked library (that is, not a UI app), add the following key-value pair to an .editorconfig file in your project:
dotnet_code_quality.CA2007.output_kind = ConsoleApplication, DynamicallyLinkedLibrary
We can have our cake and eat it to. This can be enabled on a per project type, per MS's docs:
You can configure whether you want to exclude asynchronous methods that don't return a value from this rule. To exclude these kinds of methods, add the following key-value pair to an .editorconfig file in your project:
# Package version 2.9.0 and later dotnet_code_quality.CA2007.exclude_async_void_methods = true
# Package version 2.6.3 and earlier dotnet_code_quality.CA2007.skip_async_void_methods = true
You can also configure which output assembly kinds to apply this rule to. For example, to only apply this rule to code that produces a console application or a dynamically linked library (that is, not a UI app), add the following key-value pair to an .editorconfig file in your project:
dotnet_code_quality.CA2007.output_kind = ConsoleApplication, DynamicallyLinkedLibrary
This was the same article i just read, you beat me to it :)
Notes for output_kind types:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.outputkind?view=roslyn-dotnet
since the rule only makes sense on some projects the decision is to Set to: root/suggestion
Set to NONE for test projects