Skip to main content
Source: Al0105AvoidBlockingCallsInAsyncAnalyzer.cs

Description

Calling .Result, .Wait(), or .GetAwaiter().GetResult() inside an async method can cause deadlocks, especially in contexts with a SynchronizationContext.

Bad Code

async Task<string> GetData()
{
    var result = httpClient.GetStringAsync("/api").Result;
    return result;
}

Good Code

async Task<string> GetData()
{
    var result = await httpClient.GetStringAsync("/api");
    return result;
}

Properties

  • Category: Threading
  • Severity: Warning
  • Enabled by default: True
  • Code fix available: False

Configuration

dotnet_diagnostic.AL0105.severity = warning