C#
asynchronous programming
web API
task creation
async/await

Correct way to create a new task and call asynchronously a web api c

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

When you call a web API in C#, the correct asynchronous pattern is usually not "create a new task first." Network I/O is already naturally asynchronous, so the normal approach is to make your method async and await the HttpClient call directly.

This distinction matters because Task.Run and manually created Task instances solve a different problem. They are useful for CPU-bound work, not for wrapping an already asynchronous web request.

Use async and await directly with HttpClient

For an HTTP call, the standard pattern looks like this:

csharp
1using System;
2using System.Net.Http;
3using System.Threading;
4using System.Threading.Tasks;
5
6public sealed class WeatherClient
7{
8    private readonly HttpClient _httpClient;
9
10    public WeatherClient(HttpClient httpClient)
11    {
12        _httpClient = httpClient;
13    }
14
15    public async Task<string> GetForecastAsync(CancellationToken cancellationToken)
16    {
17        using var response = await _httpClient.GetAsync(
18            "weather/today",
19            cancellationToken
20        );
21
22        response.EnsureSuccessStatusCode();
23        return await response.Content.ReadAsStringAsync(cancellationToken);
24    }
25}

This is already asynchronous. No extra Task.Run is needed because GetAsync returns a Task<HttpResponseMessage> and does not block while waiting for the network.

Do not wrap async I/O in Task.Run

Developers often write something like this:

csharp
var result = await Task.Run(() => httpClient.GetStringAsync(url));

That is usually unnecessary. It adds scheduling overhead and can make the code harder to reason about without improving scalability. The right version is simply:

csharp
var result = await httpClient.GetStringAsync(url);

Task.Run is appropriate when you need to move CPU-heavy work off the caller thread, not when the underlying operation is already non-blocking I/O.

Return Task from your own async methods

If your method performs asynchronous work, let it return Task or Task<T>. Do not block on the result with .Result or .Wait().

csharp
1public async Task ProcessAsync(CancellationToken cancellationToken)
2{
3    var json = await _httpClient.GetStringAsync(
4        "orders/pending",
5        cancellationToken
6    );
7
8    Console.WriteLine(json);
9}

This lets callers continue the async chain naturally:

csharp
await service.ProcessAsync(cancellationToken);

That is the real "correct way" to create asynchronous flow in modern C#.

Use Task.Run only for CPU-bound follow-up work

Sometimes the HTTP call is asynchronous, but the processing after it is expensive. In that case, separate the concerns:

csharp
1public async Task<int> DownloadAndParseAsync(CancellationToken cancellationToken)
2{
3    var json = await _httpClient.GetStringAsync(
4        "orders/pending",
5        cancellationToken
6    );
7
8    return await Task.Run(() => ExpensiveParse(json), cancellationToken);
9}
10
11private static int ExpensiveParse(string json)
12{
13    // Simulate CPU-heavy parsing work.
14    return json.Length;
15}

Here Task.Run is used for CPU work, not for the HTTP request itself.

Prefer injected HttpClient

Another important part of the pattern is HttpClient lifetime. Create it once or inject it through dependency injection. Do not create a new HttpClient per request unless you have a specific reason.

csharp
1builder.Services.AddHttpClient<WeatherClient>(client =>
2{
3    client.BaseAddress = new Uri("https://api.example.com/");
4    client.Timeout = TimeSpan.FromSeconds(10);
5});

This keeps connection reuse, DNS handling, and configuration under control. It also makes testing easier.

Common Pitfalls

The most common mistake is wrapping an async HTTP call inside Task.Run or manually constructing a Task. That does not make the network call more asynchronous. It just adds overhead.

Another issue is blocking on async work with .Result or .Wait(). In UI and older ASP.NET environments, that can cause deadlocks or thread starvation.

Developers also often create and dispose HttpClient for every request. That can lead to connection management problems and worse performance under load.

Finally, do not confuse asynchronous programming with parallel programming. Async HTTP calls are about not blocking while waiting for I/O. They are not automatically about spinning up more threads.

Summary

  • For web API calls, use async and await directly on HttpClient.
  • Do not create a new Task just to call an already asynchronous API.
  • Reserve Task.Run for CPU-bound work, not network I/O.
  • Return Task or Task<T> from your own async methods and let callers await them.
  • Reuse or inject HttpClient instead of constructing one per request.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design