You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to use the feature in aspnetcore Http.Request to begin processing the entity body before the complete contents of the body have been received. #43084
Recently, I want to migrate the web application from .NetFramework 4.5 to .Net Core 6. In the old code, it used the HttpRequest.GetBufferlessInputStream methd to get stream immediately before the complete contents of the body have been received. This feature can be useful if the request is uploading a large file in .Net Framework. GetBufferlessInputStream use ReadEntityBodyMode to check the entity body has been read.
However, I don't find the correspond method in Aspnetcore Http repo. GetBufferlessInputStream and ReadEntityBodyMode API have been deprecated in .Net core. And the Dotnet systemweb-adapters hasn't completed that adapter.
I would like to know if the entity-body in .Net core can be got immediately like GetBufferlessInputStream in .Net Framework and if so, how can I use it.
changed the title [-]How to use the feauture in aspnetcore Http.Request to begin processing the entity body before the complete contents of the body have been received.[/-][+]How to use the feature in aspnetcore Http.Request to begin processing the entity body before the complete contents of the body have been received.[/+]on Aug 4, 2022
I'll let other folks confirm. But I don't believe we buffer the request body entirely before making it available to the user. I believe we do some buffering for performance reasons, but that's it.
Thanks for your suggestion. I learn about that the ASP.NET Framework has a method called HttpRequest.GetBufferedInputStream,which can read stream into storage that can be read mutiple times. The GetBufferedInputStream and HttpRequestRewindExtensions.EnableBuffering in ASP.NET core maybe are the similar way to buffer the request body in default?
So both GetBufferedInputStream and GetBufferlessInputStream can buffer the request body into memory, the difference is that GetBufferlessInputStream can read immediately and GetBufferedInputStream can write the stream into disk?
Are you asking about ASP.NET Core behavior or .NET Framework behavior? On ASP.NET Core, nothing is buffered or re-playable by default. When you read the request body it's the live, unbuffered network stream. It cannot be read again. That's equivalent to GetBufferlessInputStream on .NET Framework.
If you want to support rewinding the stream then you need to call EnableBuffering. That will let you consume the stream and it will buffer the stream in memory or to disk depending on the size threshold specified (default in 30KB in memory before it flips to disk). When you enable buffering, it also means you can reset and re-read the request body.
Yeah, very nice answer for me, thank you very much.
I read the official doc about GetBufferlessInputStream on .NET Framework. As follows:
This method provides an alternative to using the InputStream property. The InputStream property waits until the whole request has been received before it returns a Stream object. In contrast, the GetBufferlessInputStream method returns the Stream object immediately. You can use the method to begin processing the entity body before the complete contents of the body have been received.
Specially, the InputStream property in .NET Framework waits the whole request. What is the property equivalent in .NET Core to InputStream in .NET Framework? This is my last question on this Issue. Thank you.
Activity
[-]How to use the feauture in aspnetcore Http.Request to begin processing the entity body before the complete contents of the body have been received.[/-][+]How to use the feature in aspnetcore Http.Request to begin processing the entity body before the complete contents of the body have been received.[/+]javiercn commentedon Aug 4, 2022
@BoltsLei thanks for contacting us.
I'll let other folks confirm. But I don't believe we buffer the request body entirely before making it available to the user. I believe we do some buffering for performance reasons, but that's it.
davidfowl commentedon Aug 4, 2022
That's right, the default behavior of ASP.NET Core does not buffer the request body, you need to opt into buffering using https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httprequestrewindextensions.enablebuffering?view=aspnetcore-6.0.
BoltsLei commentedon Aug 4, 2022
Thanks for your suggestion. I learn about that the ASP.NET Framework has a method called
HttpRequest.GetBufferedInputStream,which can read stream into storage that can be read mutiple times. TheGetBufferedInputStreamandHttpRequestRewindExtensions.EnableBufferingin ASP.NET core maybe are the similar way to buffer the request body in default?So both GetBufferedInputStream and GetBufferlessInputStream can buffer the request body into memory, the difference is that GetBufferlessInputStream can read immediately and GetBufferedInputStream can write the stream into disk?
davidfowl commentedon Aug 4, 2022
Are you asking about ASP.NET Core behavior or .NET Framework behavior? On ASP.NET Core, nothing is buffered or re-playable by default. When you read the request body it's the live, unbuffered network stream. It cannot be read again. That's equivalent to GetBufferlessInputStream on .NET Framework.
If you want to support rewinding the stream then you need to call EnableBuffering. That will let you consume the stream and it will buffer the stream in memory or to disk depending on the size threshold specified (default in 30KB in memory before it flips to disk). When you enable buffering, it also means you can reset and re-read the request body.
Does that answer your question?
BoltsLei commentedon Aug 4, 2022
Yeah, very nice answer for me, thank you very much.
I read the official doc about
GetBufferlessInputStreamon .NET Framework. As follows:Specially, the
InputStreamproperty in .NET Framework waits the whole request. What is the property equivalent in .NET Core to InputStream in .NET Framework? This is my last question on this Issue. Thank you.davidfowl commentedon Aug 4, 2022
There is nothing that behaves like the InputStream property by default in ASP.NET Core