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

wallah
  • 1,964
  • 5
  • 27
  • 48
  • 1
    you can try this code within the `UploadIFormFile` function https://github.com/neville-nazerane/netcore.azure.blob/blob/master/NetCore.Azure.Blob/BlobManagerExtension.cs – Neville Nazerane Apr 27 '18 at 05:15
  • @NevilleNazerane Thanks ! I will reference it when I do refactoring! – wallah Apr 27 '18 at 06:17

1 Answers1

3

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.

wallah
  • 1,964
  • 5
  • 27
  • 48
  • 4
    Async void runs your method in the background so your request ends without waiting for it. You should almost never use async void. – Tratcher Apr 27 '18 at 03:22
  • @Tratcher Thank you tell me the reason! I will never use like that :) – wallah Apr 27 '18 at 03:23