Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does IFormFile.OpenReadStream() need to be disposed?

Tags:

c#

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

like image 881
Josh Avatar asked Dec 04 '25 22:12

Josh


1 Answers

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.

like image 114
Josh Avatar answered Dec 06 '25 12:12

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!