CA2007: Do not directly await a Task

TypeName DoNotDirectlyAwaitATaskAnalyzer
CheckId CA2007
Category Microsoft.Reliability
Breaking change Non-breaking

Cause

An asynchronous method awaits a Task directly.

Rule description

When an asynchronous method awaits a Task directly, continuation occurs in the same thread that created the task. This behavior can be costly in terms of performance and can result in a deadlock on the UI thread. Consider calling Task.ConfigureAwait(Boolean) to signal your intention for continuation.

This rule was introduced with FxCop analyzers and doesn't exist in legacy FxCop analysis.

How to fix violations

To fix violations, call ConfigureAwait on the awaited Task. You can pass either true or false for the continueOnCapturedContext parameter.

  • Calling ConfigureAwait(true) on the task has the same behavior as not explicitly calling ConfigureAwait. By explicitly calling this method, you're letting readers know you intentionally want to perform the continuation on the original synchronization context.

  • Call ConfigureAwait(false) on the task to schedule continuations to the thread pool, thereby avoiding a deadlock on the UI thread. Passing false is a good option for app-independent libraries.

When to suppress warnings

You can suppress this warning if you know that the consumer is not a graphical user interface (GUI) app or if the consumer does not have a SynchronizationContext.

Example

The following code snippet generates the warning:

public async Task Execute()
{
    Task task = null;
    await task;
}

To fix the violation, call ConfigureAwait on the awaited Task:

public async Task Execute()
{
    Task task = null;
    await task.ConfigureAwait(false);
}

Configurability

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

For more information, see Configure FxCop analyzers.

See also