Skip to main content
Source: Al0106AvoidTaskRunInAspNetCoreAnalyzer.cs

Description

ASP.NET Core request handlers already execute on the thread pool. Using Task.Run() adds unnecessary overhead by scheduling work to another thread pool thread.

Bad Code

app.MapGet("/orders", async () =>
{
    return await Task.Run(() => db.Orders.ToList());
});

Good Code

app.MapGet("/orders", async (AppDbContext db) =>
{
    return await db.Orders.ToListAsync();
});

Properties

  • Category: ASP.NET Core
  • Severity: Warning
  • Enabled by default: True
  • Code fix available: False

Configuration

dotnet_diagnostic.AL0106.severity = warning