Search code examples
c#.netasync-awaittaskhttpclient

Why .NET HttpResponseMessage.Content methods for reading are async?


I noticed, that HttpResponseMessage.Content has ReadAsStringAsync() method. What is the sense of making it async when this operation requires cpu and task reation will add more CPU work?


Solution

  • It's not purely a CPU-bound operation. In a request with a large response, you can start reading the response body before the entire response body has been received over the network.

    The first part of the response will likely already be in memory, but you could very well hit a point while reading the response where you need to wait for the rest of the data to be received over the network. That allows you to wait asynchronously and not block the thread.