✅ Is this necessary?

If using disposes of everything inside of it, why is this still a thing?
using (HttpClient client = new HttpClient(handler, true))
using (HttpResponseMessage resp = await client.GetAsync()) {
}
using (HttpClient client = new HttpClient(handler, true))
using (HttpResponseMessage resp = await client.GetAsync()) {
}
Wont HttpResponseMessage be disposed of once the client is finished anyway? o.O Just wanting to understand it better, idk what other channels to use
5 Replies
UnemployedNinja
UnemployedNinja3mo ago
I've seen this around a few times when I was looking into how to use the HttpClient and am only know thinking about it, after learning a little more
not guilty
not guilty3mo ago
HttpClient could be (and should be) long lived so you could have many HttpResponseMessages
UnemployedNinja
UnemployedNinja3mo ago
Seems so simple put like that... nvm then lol ty
Sossenbinder
Sossenbinder3mo ago
I think disposing of the HttpClient will not dispose the response messages it "generated". Disposing the response message in turn will dispose underlying streams it keeps open. But I think it's actually relatively rare you need to work at this level, because most of the higher level abstractions around HttpClient will handle this for you, and only return e.g. the string to be converted to json
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View