Comment

Comment by rianjs

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);
  }
parent rianjs 10 years ago

No comments yet

Be the first to share your thoughts.

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton