Skip to content

Instantly share code, notes, and snippets.

@JoanM
Created December 20, 2016 11:05
Show Gist options
  • Save JoanM/15a956b968fca89fc0037240ed97b361 to your computer and use it in GitHub Desktop.
Save JoanM/15a956b968fca89fc0037240ed97b361 to your computer and use it in GitHub Desktop.
DoCurlAsync with only one ConfigureAwait
public async Task<string> DoCurlAsync()
{
using (var httpClient = new HttpClient())
using (var httpResonse = await httpClient.GetAsync("https://www.bynder.com").ConfigureAwait(false))
{
return await httpResonse.Content.ReadAsStringAsync();
}
}
@kiquenet
Copy link

In .NET, as Simon Timms described in his article, you have to be careful when using the HTTPClient class.
Continuous instantiation and disposal of the HTTPClient object may create a socket exhaustion on your machine and affect performance.
Consider reusing the HTTPClient object throughout your calls for better performance.

https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
https://ankitvijay.net/2016/09/25/dispose-httpclient-or-have-a-static-instance/

@MikeVelso
Copy link

Actually @JoanM you are incorrect.
Using ConfigureAwait(false) is an anti pattern.
If your application is x async methods deep, you would need to add ConfigureAwait(false) to each and every awaitable down the call stack; otherwise you risk using a method incorrectly sooner or later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment