Skip to content

Failed when MemoryStream is wrapped inside a BufferedReadStream  #9775

@kulkarnimeghak

Description

@kulkarnimeghak

Description

Creating a file with seeking support, combined with BufferedReaderStream failing with error message "The value must be greater than zero. Parameter name: count"

To Reproduce

Steps to reproduce the behavior:

  1. Using ASP.NET Core '2.1'
  2. Add below middleware
app.Use (async (context, next) =>
             {
                // Create a copy of current request stream and save it as MemoryStream,
                // Other stream classes can be used as well, as long as they support seeking.
                // In other words, CanSeek must return true.
                var stream = new MemoryStream ();
                context.Request.Body.CopyTo (stream);
                stream.Position = 0; // Rewinding stream

               // Wrap copied stream with BufferedReadStream
               // and set as new body stream.
               // This step is important for reproduction.
               context.Request.Body = new BufferedReadStream (stream, 81920);
               context.Response.RegisterForDispose (context.Request.Body);
               await next.Invoke ();
             });
  1. Sends the HTTP request to upload file to azure blob storage (I'm using Azure storage emulator)
POST http://localhost:5000/api/files/upload HTTP/1.1
Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
User-Agent: Fiddler
Host: localhost:5000
Content-Length: 224

---------------------------acebdf13572468
Content-Disposition: form-data; name="fieldNameHere"; filename="TestfileWithdata.txt"
Content-Type: text/plain

test file with data
---------------------------acebdf13572468--
  1. See error
    Status code :400
    error message: The value must be greater than zero.Parameter name: count
    stack trace: 
at Microsoft.AspNetCore.WebUtilities.BufferedReadStream.ValidateBuffer(Byte[] buffer, Int32 offset, Int32 count)
   at Microsoft.AspNetCore.WebUtilities.BufferedReadStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
   at Microsoft.WindowsAzure.Storage.Core.Util.StreamExtensions.WriteToAsync[T](Stream stream, Stream toStream, IBufferManager bufferManager, Nullable`1 copyLength, Nullable`1 maxLength, Boolean calculateMd5, ExecutionState`1 executionState, StreamDescriptor streamCopyState, CancellationToken token) in C:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\Common\Core\Util\StreamExtensions.cs:line 301
   at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamAsyncHelper(Stream source, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, IProgress`1 progressHandler, CancellationToken cancellationToken) in C:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\WindowsRuntime\Blob\CloudBlockBlob.cs:line 352
   at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamAsyncHelper(Stream source, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken) in C:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\WindowsRuntime\Blob\CloudBlockBlob.cs:line 290
   at SampleMSWrapBRS.Helpers.StorageHelper.UploadFileToStorage(Stream fileStream, String fileName, AzureStorageConfig storageConfig) in D:\Learning\2019Q1\MSWrapBRS\WebApplication\SampleMSWrapBRS\StorageHelper.cs:line 31
   at SampleMSWrapBRS.Controllers.FilesController.Upload(ICollection`1 files) in D:\Learning\2019Q1\MSWrapBRS\WebApplication\SampleMSWrapBRS\Controllers\FilesController.cs:line 43

Expected behavior

Should allow uploading file to Azure blob storage.

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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions