azure-storage-net: storageAsyncResult.End(); throws NullReferenceException.. most times.

Running the following code some images work to upload and others don’t. I have tried to figure out what differs them but an image that works to upload can later fail and vice versa.

This is the code I use to upload blobs:

public async Task UploadAndSaveBlobAsync(string adId, IFormFile imageFile, string fileName){
            var blobName = adId + "/" + fileName;
            await _container.CreateIfNotExistsAsync();
            await _container.SetPermissionsAsync(new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });
            var blockBlob = _container.GetBlockBlobReference(blobName);
            blockBlob.Properties.ContentType = imageFile.ContentType;

            try
            {
                using (var stream = imageFile.OpenReadStream())
                {
                    await blockBlob.UploadFromStreamAsync(stream);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("Could not upload image" + e.Message);
                throw;
            }

            var blobsName = blockBlob.Uri.ToString();

        }

/End example code

And here is the StackTrace for "“Object reference not set to an instance of an object.”

" at Microsoft.WindowsAzure.Storage.Core.Util.StorageAsyncResult`1.End() in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Util\StorageAsyncResult.cs:line 77\r\n at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.EndUploadFromStream(IAsyncResult asyncResult) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 739\r\n at Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.<>c__DisplayClass4.<CreateCallbackVoid>b__3(IAsyncResult ar) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Util\AsyncExtensions.cs:line 114\r\n— End of stack trace from previous location where exception was thrown —\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at Mulimo.DAL.BlobStorage.<UploadAndSaveBlobAsync>d__7.MoveNext() in C:\Source\Mulimo\src\Mulimo.DAL\BlobStorage.cs:line 70"

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 34 (12 by maintainers)

Commits related to this issue

Most upvoted comments

I’m not sure if this is expected to work too (considering https://github.com/aspnet/HttpAbstractions/issues/602) but it seems to be an alternative solution using streams.

using (var imageStream = image.OpenReadStream())
{
    using (var memStream = new MemoryStream((int)image.Length))
    {
        await imageStream.CopyToAsync(memStream);
        memStream.Position = 0;
        await blockBlob.UploadFromStreamAsync(memStream);
    }
}

I’m not expecting any evident improvement over using UploadFromByteArrayAsync, obviously.

I can reproduce this problem every time when I use mono and want to use the Azure Storage on both Windows and Ubuntu 14.04 (didn’t test Mac OS X). The problem exists since (at least 3.2.1). I didn’t test older versions.

Microsoft.WindowsAzure.Storage.StorageException: Object reference not set to an instance of an object ---> System.NullReferenceException: Object reference not set to an instance of an object
  at System.Net.WebConnectionStream.EndRead (IAsyncResult r) <0xb804200 + 0x00061> in <filename unknown>:0
  at Microsoft.WindowsAzure.Storage.Core.ByteCountingStream.EndRead (IAsyncResult asyncResult) <0xb82e858 + 0x0001a> in <filename unknown>:0
  at Microsoft.WindowsAzure.Storage.Core.Util.AsyncStreamCopier`1[T].ProcessEndRead () <0xb836b70 + 0x0002d> in <filename unknown>:0
  at Microsoft.WindowsAzure.Storage.Core.Util.AsyncStreamCopier`1[T].EndOperation (IAsyncResult res) <0xb835fd8 + 0x00053> in <filename unknown>:0
  at Microsoft.WindowsAzure.Storage.Core.Util.AsyncStreamCopier`1[T].EndOpWithCatch (IAsyncResult res) <0xb835d30 + 0x00063> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Microsoft.WindowsAzure.Storage.Core.Util.StorageAsyncResult`1[T].End () <0xb84df40 + 0x0003f> in <filename unknown>:0
  at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.EndUploadFromStream (IAsyncResult asyncResult) <0xb84def8 + 0x0002f> in <filename unknown>:0
  at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.EndUploadFromByteArray (IAsyncResult asyncResult) <0xb84ded0 + 0x00017> in <filename unknown>:0
  at Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions+<>c__DisplayClass4.<CreateCallbackVoid>b__3 (IAsyncResult ar) <0xb84dc98 + 0x00095> in <filename unknown>:0

EDIT: Added stack trace EDIT 2: Using UploadFromByteArray instead of UploadFromByteArrayAsync works. It seems that - when I use the *Async methods, that a timeout is triggered (after ~30 seconds) that causes the NRE when the request ends.