Does it make sense to use the using pattern in this case? Or does await automatically dispose of this resource?
var payload = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
Does it make sense to use the using pattern in this case? Or does await automatically dispose of this resource?
var payload = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
T McKeown
On
No, await does no such thing.
But it makes sense to wrap streams that your code creates in a using() { ... } or a try/finally block, Yes. However, if the stream was not "created" by your code then you should feel no obligation to dispose or close it.
You could easily do this:
using (StreamReader s = new StreamReader(HttpContext.Request.Body))
{
var payload = await s.ReadToEndAsync();
}
Copyright © 2021 Jogjafile Inc.
No, and why would it?
Look at the whole StreamReader API - it i s not ONLY "ReadToEnd" - and even then, what if your code then RESETS the reader to a different position and reads more?
It would totally go against the API of a StreamReader to automatically dispose it and actively sabotage it in many use cases.
Also, it would make an async method behave different form the non-async version.
Ergo - no, it does make no sense for it to dispose, so it does not.