none
CloudBlockBlob.UploadFromFileAsync Cancel?

    Question

  • Has anyone actually been successful cancelling an asynchronous upload that was started using CloudBlockBlob.UploadFromFileAsync?  My efforts so far have been futile.  I execute the "Cancel" method on the "CancellationTokenSource", but the CloudBlockBlob just seems to ignore it.  If someone could produce a working (tested) code snippet, that would be awesome.  Any help would be appreciated!

    Thanks!

    Brad.

    Saturday, November 28, 2015 2:23 AM

All replies

  • Because it is an async operation don't expect that the cancellation request to be executed in the next second. You should allow some time to pass. Take a look on http://stackoverflow.com/questions/28580083/how-to-wait-synchronously-on-cancellation-of-async-task
    Saturday, November 28, 2015 9:05 AM
  • I understand the nature of asynchronous operations:  I realize it may not happen immediately.  However, in my case it NEVER happens.  I've tried uploading a file that is 500MB which, with my connection speed, can take a few minutes.  I cancel almost immediately and it never gets cancelled.  The upload continues to completion, and then the completion status comes back as cancelled.  I can understand it taking a few seconds to respond, but the observed behavior is not what I would expect.  :-)

    So my question is "have you tried it?"  Does it work for you?  I'd be happy to hear it worked for you, because that would mean I'm doing something wrong and that it could be fixed.  If necessary, I can provide my code - I just want to get this working.

    Thanks!

    Brad.

    Saturday, November 28, 2015 7:15 PM
  • Just tested and works great. Below you can find the code that I used. I have VS2015 and the last version of Azure SDK. Could you share the code with us?

       class Program
        {
            public static void UploadToBlobWithCancelation(string fullFilePath)
            {
                string connectionString = @"DefaultEndpointsProtocol=https;AccountName=XXXX;AccountKey=XXXXX+==";
                CloudStorageAccount account = CloudStorageAccount.Parse(connectionString);
                CloudBlobClient client = account.CreateCloudBlobClient();
                CloudBlobContainer container = client.GetContainerReference("upload");
                container.CreateIfNotExists();
                CloudBlockBlob blob = container.GetBlockBlobReference("uploadedContent");
                BlobContainerPermissions containerPermissions = new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob };
                container.SetPermissions(containerPermissions);
    
                CancellationTokenSource cancellationToken = new CancellationTokenSource();
                Task task = blob.UploadFromFileAsync(fullFilePath, System.IO.FileMode.Open, cancellationToken.Token);
                cancellationToken.Cancel();
    
                // At the next line of code 'task.Wait()' you should receive an Exception that has inside a cancelation exception.
                //task.Wait();
            }
            static void Main(string[] args)
            {
                UploadToBlobWithCancelation(@"C:\Users\XXX\Downloads\1M_BR.pptx");
    }

    • Proposed as answer by Radu VunvuleaMVP Saturday, November 28, 2015 9:19 PM
    • Unproposed as answer by Pletzky Monday, November 30, 2015 10:34 PM
    Saturday, November 28, 2015 9:19 PM
  • Ummm....  Have you actually tried the code you propose?  Does it work for you?  It doesn't work for me.  It exhibits exactly the same problem as mine.  The upload does not stop until it's done.

    Brad.

    Monday, November 30, 2015 10:37 PM
  • P.S.  Here is the complete source.  I've only changed the constants:

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Blob;
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace AzureAsyncUpload01
    {
        class Program
        {
            private const string CONNECTION_STRING = @"?";
            private const string CONTAINER_NAME = "??";
            private const string BLOB_NAME = "???";
            private const string FILE_NAME = @"big_file.bin";
    
            static void Main(string[] args)
            {
                UploadToBlobWithCancelation(FILE_NAME);
            }
    
            private static CloudBlobContainer GetBlobContainer()
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CONNECTION_STRING);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference(CONTAINER_NAME);
    exist.
                container.CreateIfNotExists();
    
                return container;
            }
    
            public static void UploadToBlobWithCancelation(string fullFilePath)
            {
                try
                {
                    CloudBlobContainer container = GetBlobContainer();
                    container.CreateIfNotExists();
                    CloudBlockBlob blob = container.GetBlockBlobReference(BLOB_NAME);
                    BlobContainerPermissions containerPermissions = new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob };
                    container.SetPermissions(containerPermissions);
    
                    CancellationTokenSource cancellationToken = new CancellationTokenSource();
                    Task task = blob.UploadFromFileAsync(fullFilePath, System.IO.FileMode.Open, cancellationToken.Token);
                    cancellationToken.Cancel();
    
                    // At the next line of code 'task.Wait()' you should receive an Exception that has inside a cancelation exception.
                    task.Wait();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Houston...");
                    Console.WriteLine(ex.ToString());
                }
    
                Console.Write("Hit [Enter] to continue: ");
                Console.ReadLine();
            }
        }
    }
    
    Brad.

    Monday, November 30, 2015 10:43 PM
  • Could you provide more information related to:

    • .NET Version, Nuget versions, VS version

    It seems that you have a 'special' configuration. The problem that you it seems that can be reproduced only on your machine. I would recommend to check on another machine. And yes, the code works on my machine.

    If the problem persisted on your machine try to install the last versions of Azure SDK.  

    Wednesday, December 2, 2015 7:34 AM
  • OK, that's actually good news, as far as I'm concerned.  I do have the latest versions of everything installed, but I'll try it on another machine before I pursue this further.

    Thanks a LOT for the confirmation!  As soon as I can confirm this works, I'll mark your response as the answer.  :-)

    Brad.

    Wednesday, December 2, 2015 5:12 PM
  • Well, I'm disappointed. I've now tried it on multiple machines, and it's still not working. I don't want to doubt that it works on my machine, but at the same time I wonder.  The thing is that it appears to work when uploading small files.  However, large files don't actually stop uploading.  Once the upload is complete, the exception is raised, but not until the upload is finished.

    Here are my NuGet packages:

    NuGet Packages

    Any more ideas?

    Thanks again!

    Brad.

    Wednesday, December 2, 2015 5:52 PM