Should IFormFile.OpenReadStream() be called inside a using block so it's properly disposed of? Or will it be disposed of by the IFormFile after the http request finishes processing?
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.iformfile.openreadstream?view=aspnetcore-5.0#Microsoft_AspNetCore_Http_IFormFile_OpenReadStream
The default implementation of FormFile creates a new ReferenceReadStream every time OpenReadStream() is called:
https://github.com/dotnet/aspnetcore/blob/033b1fb1cf681ea95d3954c08e4391c93cd72683/src/Http/Http/src/FormFile.cs#L81
ReferenceReadStream does not contain any unmanaged resources. Calling Dispose on it is essentially a no-op.
https://github.com/dotnet/aspnetcore/blob/033b1fb1cf681ea95d3954c08e4391c93cd72683/src/Http/Http/src/Internal/ReferenceReadStream.cs#L14
With that in mind, IFormFile.OpenReadStream() doesn't need to be disposed. But disposing it also doesn't hurt anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With