Open Closed

why when i upload multiple files using i remoteStream not working and throw exception System.InvalidOperationException: 'The inner stream position has changed unexpectedly.' #8786


User avatar
0
a.mourad created
    public virtual async Task<List<AppFileDescriptorDto>> UploadFilesAsync(List<IRemoteStreamContent> inputs)
    {
        var uploadedFiles = new List<AppFileDescriptorDto>();

        foreach (var input in inputs)
        {
            var id = GuidGenerator.Create();
            var fileDescriptor = new AppFileDescriptors.AppFileDescriptor(id, input.FileName, input.ContentType);

            try
            {
                byte[] fileBytes;

                // Read the stream **once** into a byte array
                await using (var originalStream = input.GetStream())
                {
                    if (originalStream == null || !originalStream.CanRead)
                    {
                        throw new InvalidOperationException("Invalid stream: The stream is null or unreadable.");
                    }

                    using (var memoryStream = new MemoryStream())
                    {
                        await originalStream.CopyToAsync(memoryStream);
                        fileBytes = memoryStream.ToArray(); 
                    }
                } 

                // Insert file metadata into database
                await _appFileDescriptorRepository.InsertAsync(fileDescriptor);

                // Create a fresh stream from the byte array for saving
                await using (var finalStream = new MemoryStream(fileBytes))
                {
                    await _blobContainer.SaveAsync(fileDescriptor.Id.ToString("N"), finalStream);
                }

                // Map to DTO
                var dto = ObjectMapper.Map<AppFileDescriptors.AppFileDescriptor, AppFileDescriptorDto>(fileDescriptor);
                uploadedFiles.Add(dto);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error processing file {input.FileName}: {ex.Message}");
                throw;
            }
        }

        return uploadedFiles;
    }

1 Answer(s)
  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    Hello a.mourad,

    IRemoteStreamContent interface is as follows:

    public interface IRemoteStreamContent : IDisposable
    {
        string? FileName { get; }
    
        string ContentType { get; }
    
        long? ContentLength { get; }
    
        Stream GetStream();
    }
    

    You can see that it contains streams.

    Actually the exception is not thrown by ABP but by .NET. Here is why: https://stackoverflow.com/questions/55674322/getting-the-inner-stream-position-has-changed-unexpectedly-in-aws-lambda/55674833#55674833

    Here's where the exception was thrown: https://github.com/dotnet/aspnetcore/blob/81b289460a9ce5fcbd0d32d80a645f139dffc422/src/Http/Http/src/Internal/ReferenceReadStream.cs#L70

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 20, 2025, 18:00