Skip to content

Repository files navigation

ConfigureAwait.Fody

Build statusNuGet Status

Configure async code's ConfigureAwait at a global level.

See Milestones for release notes.

This is an add-in for Fody

It is expected that all developers using Fody become a Patron on OpenCollective. See Licensing/Patron FAQ for more information.

Usage

See also Fody usage.

NuGet package

Install the ConfigureAwait.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package ConfigureAwait.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

How to use it

By default, ConfigureAwait.Fody doesn't change any code. Set a configure await value at the assembly, class, or method level.

  • [assembly: Fody.ConfigureAwait(false)] - Assembly level
  • [Fody.ConfigureAwait(false)] - Class or method level

Explicitly configured awaiters will not be overwritten by the weaver, allowing exceptions to the Assembly / Class / Method level setting.

Add to FodyWeavers.xml

Add <ConfigureAwait/> to FodyWeavers.xml

<Weavers>
<ConfigureAwait/>
</Weavers>

It is also possible set the default ContinueOnCapturedContext in the xml config:

<Weavers>
<ConfigureAwaitContinueOnCapturedContext="false" />
</Weavers>

Configure once for a whole solution

To share the same setting across every project in a solution, instead of adding the attribute or a FodyWeavers.xml to each project, use Fody's WeaverConfiguration MSBuild property in a Directory.Build.props at the root of the solution:

<Project>
<PropertyGroup>
<WeaverConfiguration>
<Weavers>
<ConfigureAwaitContinueOnCapturedContext="false" />
</Weavers>
</WeaverConfiguration>
</PropertyGroup>
</Project>

WeaverConfiguration overrides any per-project FodyWeavers.xml, and supports MSBuild property interpolation ($(SomeProperty)) and Condition attributes, so the value can vary by build configuration.

Example

Before code

usingFody;[ConfigureAwait(false)]publicclassMyAsyncLibrary{publicasyncTaskMyMethodAsync(){awaitTask.Delay(10);awaitTask.Delay(20).ConfigureAwait(true);}publicasyncTaskAnotherMethodAsync(){awaitTask.Delay(30);}}

What gets compiled

publicclassMyAsyncLibrary{publicasyncTaskMyMethodAsync(){awaitTask.Delay(10).ConfigureAwait(false);awaitTask.Delay(20).ConfigureAwait(true);}publicasyncTaskAnotherMethodAsync(){awaitTask.Delay(30).ConfigureAwait(false);}}

Icon

Created by Dmitry Baranovskiy from The Noun Project.

About

Configure async code's ConfigureAwait at a global level

Resources

Security policy

Stars

458 stars

Watchers

3 watching

Forks

Sponsor this project

Used by

Contributors

Languages