Skip to content

Use in library code

Context: Library code should use ConfigureAwait(false) for all await statements unless it explicitly needs to resume on the original context.

using System.Net.Http;
using System.Threading.Tasks;
public class ApiClient
{
private readonly HttpClient _httpClient = new HttpClient();
public async Task<string> ReadDataAsync(string url)
{
// ConfigureAwait(false) for all awaits in library code
string content = await _httpClient.GetStringAsync(url).ConfigureAwait(false);
return await ProcessAsync(content).ConfigureAwait(false);
}
private async Task<string> ProcessAsync(string data)
{
await Task.Delay(10).ConfigureAwait(false);
return data.ToUpper();
}
}
  • Prevents deadlocks when library is used from UI applications.
  • Improves performance by reducing unnecessary context switches.

NuGet packages: Well‑written libraries like Newtonsoft.Json use ConfigureAwait(false) internally.

Example: .NET Runtime library guidelines