In this code:
using (var httpClient = new HttpClient())
{
using var request = await httpClient.DeleteAsync($"https://somesite.com");
}
Is the second using
required or is it redundant?
In this code:
using (var httpClient = new HttpClient())
{
using var request = await httpClient.DeleteAsync($"https://somesite.com");
}
Is the second using
required or is it redundant?
The using
statement is there to Dispose
of the HttpClient
object once it's gone out of its scope. The second using
does the same for the HttpResponseMessage
. You can read about disposing it here, but generally this should cover it:
In the majority of cases, the body of the response from the underlying connection (Socket) is fully received, and the byte data, representing the content will be buffered into a memory stream automatically. This occurs when using most overloads of the HttpClient APIs (GetAsync, PostAsync and SendAsync).
Once data has finished buffering, the underlying connection which was used to make the request will go idle and be marked as having completed. It will also be disassociated from the HttpContentStream and therefore be available to handle further requests via the HttpClient.
When calling dispose manually on HttpResponseMessage, in the above case, since the connection is already released the call to Dispose will merely dispose of the HttpContent MemoryStream.
For best practice you can read:
The safest, general advice would be to always dispose of the HttpResponseMessage once you have finished with using it. This does lead to a little more code noise but ensures that regardless of the internals and any future changes, your code will free/clean up unused resources such as connections as quickly as possible. Be aware that once you dispose of the content, it will become unusable for anyone else who has been passed a reference to it.
That being said, I've never done it myself and never had any problems with it ;)
May I add...
The using statement is useful, but it does cause issues with HttpClient
. Please refer to this article
I used to create a IHttpClientFactory
interface with a private instance of HttpClient
within while using .NET Framework, then injecting it in API calling class(es).
Please note, that .NET Core provides a way to inject HttpClient
very easily.
Essentially, what happens is, in your Startup.cs
class
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient<OAuthClient>(client =>
{
// you can configure your httpclient here
client.BaseAddress = new Uri("https://www.example.com/api/v3");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "accessToken");
});
}
And then inject in other classes:
public class SomeClass
{
private HttpClient _client;
public SomeClass(HttpClient client)
{
_client = client;
}
public async Task GetStuffFromAPI(string requestUrl)
{
await _client.GetAsync(reqestUrl);();
}
What happens with the above is: every time you inject HttpClient
, it'll first try to inject the instance you've specified in your Startup
class. There's much more to it, but I'll keep this answer simple