Skip to content

Use ConfigureAwait(false) on awaited task #235

@thomaslevesque

Description

@thomaslevesque

Best practice dictates that ConfigureAwait(false) should always be used in library code, unless the async operation specifically needs to resume on the original SynchronizationContext. So I think it would be useful to create an analyzer for this.

  • Level: not sure whether the level should be Information or Warning.
  • Code fix: obviously, call ConfigureAwait(false) on the awaited task

Scenario

await FooAsync();

becomes

await FooAsync().ConfigureAwait(false);

Another thing to consider: should the analyzer warn only on the first awaited task in the method, or all of them? Unless I'm mistaken, if ConfigureAwait(false) is used on the first await, it's not necessary to use it on the others, since we have already "lost" the SynchronizationContext, so there isn't a SC to capture anyway.

Diagnostic Id: CC0070
Severity: Hidden (Refactoring)
Category: Reliability

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions