question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

storageAsyncResult.End(); throws NullReferenceException.. most times.

See original GitHub issue

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"

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:34 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
bragmacommented, Apr 11, 2016

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.

1reaction
fubar-codercommented, Mar 21, 2016

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NullReferenceException when returning Blob stream in ...
So after stepping thru things with a decompiler, I found the BlobReadStreamBase.Dispose() method was being called before it had been read.
Read more >
container.CreateIfNotExists() throws NullReferenceException
Hi, Downloaded latest code. Trying to run sample with my Azure account and getting following error. Caused by: java.lang.
Read more >
RadGrid filtering throws null reference exception when ...
I have a RadGrid in a RadWindow. When I try to filter the grid it does a postback and throws this exception: [NullReferenceException:...
Read more >
What is a Null Reference Exception?
NullReferenceException happens when your script code tries to use a variable which isn't set (referencing) and object. The error message that appears tells...
Read more >
How can I fix the error: System.NullReferenceException
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found