When trying to upload file, FileBufferingReadStream.ThrowIfDisposed() error occurr

1

I receive a image file with Microsoft.AspNetCore.Http.IFormFile type in Controller.

And I upload this file on Azure Blob.

Before that I take a process like following

Controller

[HttpPost]
public async void ActionMethod(IFormFile img)
{

    // some process        

    using(MemoryStream stream = new MemoryStream())
    {
        // (1)
        img.CopyTo(stream); // (2)
        stream.Seek(0, SeekOrigin.Begin);
        // call await cloud block blob.UploadFromStreamAsync(stream);
    }

    // some process
}

When get through using the stream is

stream

CanRead:    true
CanSeek:    true
CanTimeout: false
CanWrite:   true
Capacity:   0
Length:     0
Position:   0
ReadTimeout:  'stream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
WriteTimeout: 'stream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'

and at and after point (2) the following error occur

System.ObjectDisposedException occurred
  HResult=0x80131622
  Message=Cannot access a disposed object
  ObjectName: 'FileBufferingReadStream'
  Source=Microsoft.AspNetCore.WebUtilities
  StackTrace:
    at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ThrowIfDisposed()
    at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.set_Position(Int64 value)
    at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream..ctor(Stream inner, Int64 offset, Int64 length)
   at Microsoft.AspNetCore.Http.Internal.FormFile.OpenReadStream()
   at Microsoft.AspNetCore.Http.Internal.FormFile.CopyTo(Stream target)
   at AzureStorageManager.AzureStorageFileModules.<UploadFileAsync>d__11.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

The real problem is that the obove codes work well some situation but not in some(I can not catch the situation. The file is same in every time).

Please help me...

c#
asp.net-core
azure-storage
azure-storage-blobs
asked on Stack Overflow Apr 27, 2018 by wallah • edited Apr 27, 2018 by wallah

1 Answer

1

I struggle to solve this problem for a day and right after upload this question I found a solution. The problem which make this issue is the return type of Action method. If the return type is not Task<T> the error is occurred. So I fixed my Action Method like

[HttpPost]
public async Task<int> ActionMethod(IFormFile img)
{
     // same

     return resultValue;
}

After it, the error does not show up. Actually I don't know why exactlly. So If you know, tell me and share your knowledge. Thanks.

answered on Stack Overflow Apr 27, 2018 by wallah • edited Apr 27, 2018 by wallah

User contributions licensed under CC BY-SA 3.0