Skip to content

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

Closed

Description

@BoltsLei

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.

Activity

  1. 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
  2. javiercn commented on Aug 4, 2022

    @javiercn
    Member

    @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.

  3. davidfowl commented on Aug 4, 2022

    @davidfowl
    Member

    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.

  4. added this to the Discussions milestone on Aug 4, 2022
  5. BoltsLei commented on Aug 4, 2022

    @BoltsLei
    Author

    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.

    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?

  6. davidfowl commented on Aug 4, 2022

    @davidfowl
    Member

    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?

  7. BoltsLei commented on Aug 4, 2022

    @BoltsLei
    Author

    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.

  8. davidfowl commented on Aug 4, 2022

    @davidfowl
    Member

    There is nothing that behaves like the InputStream property by default in ASP.NET Core

  9. ghost locked as resolved and limited conversation to collaborators on Sep 11, 2022
  10. added
    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
    and removed on Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions