How to dispose HttpClient after it's finished?

Issue

I want to use HttpClient to get content for my web page.

public async Task<IActionResult> OnGet()
{
    NetworkCredential credentials = new NetworkCredential(Settings.Username, Settings.Password);

    using (HttpClientHandler handler = new HttpClientHandler { Credentials = credentials })
    using (HttpClient httpClient = new HttpClient(handler))
    {
        Content = await httpClient.GetStringAsync(requestUriString);
    }

    return Page();
}

The problem is that my using statements cause the HttpClient and HttpClientHandler objects to be destroyed before GetStringAsync() has completed.

I can remove the using statements, but then I can’t be assured the connections get disposed in a timely fashion.

Note that HttpClient does not implement IAsyncDisposable and so using await using is not an option.

How can I ensure my HTTP objects get disposed of as soon as possible without disposing them before the code is finished?

Solution

So HttpClient is actually a bit weird in the context of being an IDisposable. Even though it implements the IDisposable interface, Microsoft actually advises against placing it in a using statement.

If you are using it in an ASP.NET Core context, you should use the HttpClientFactory to manage the lifetime of your HttpClients. If not, it is recommended to use a single static instance, but be wary that this can cause other issues, such as DNS staleness.

Answered By – sunero4

Answer Checked By – Katrina (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.