Hacker News new | ask | show | jobs
by rianjs 3581 days ago
HttpResponseMessages also implement IDisposable, and should be disposed. HttpContent also implements IDisposable, but disposing of the parent HttpResponseMessage also disposes of the child HttpContent object as step 1, so you needn't have a second using statement.

In a typical usage scenario in an async Task, this is what it'd look like:

  using (var response = await _httpClient.GetAsync(requestUri))
  {
      response.EnsureSuccessStatusCode();
      var result = await response.Content.ReadAsStringAsync();
      return JsonConvert.DeserializeObject<Foo>(result);
  }