简体   繁体   English

UploadFromStreamAsync 取消令牌不起作用

[英]UploadFromStreamAsync Cancellation Token not working

Expected behaviour :预期行为

Looking at my internet usage in task manager after running should see a spike in upload for around 5 seconds and then a drop back to normal levels.运行后在任务管理器中查看我的互联网使用情况应该会看到上传峰值大约 5 秒,然后回落到正常水平。

Result :结果

Upload speed spikes for a lot longer (closer to a minute or more, indicative of the full file being uploaded)上传速度峰值的时间更长(接近一分钟或更长时间,表明正在上传完整文件)

Tried :试过

  • Cancelling after multiple times (eg 1 second, 10 seconds etc)多次取消(例如 1 秒、10 秒等)
  • Immediately cancelling with the token after starting the upload Using开始上传后立即使用令牌取消使用
  • UploadFromByteArrayAsync() instead of UploadFromStreamAsync() Using UploadFromByteArrayAsync() 代替 UploadFromStreamAsync() 使用
  • BeginUploadFromStream() with EndUploadFromStream() BeginUploadFromStream() 和 EndUploadFromStream()

Although I can quite easily cancel a download using the CancellationToken, no matter what I do, I can't cancel this upload.虽然我可以很容易地使用 CancellationToken 取消下载,但无论我做什么,我都无法取消此上传。 Also, weirdly, searching online, I can't find any instance of anyone else having problems cancelling an upload.另外,奇怪的是,在网上搜索,我找不到任何其他人在取消上传时遇到问题的实例。

            _connectionString = "xxx";                

            if (_connectionString != "")
            {
                _storageAccount = CloudStorageAccount.Parse(_connectionString);
                _blobClient = _storageAccount.CreateCloudBlobClient();
            }

            string ulContainerName = "speedtest";
            string ulBlobName = "uploadTestFile" + DateTime.UtcNow.ToLongTimeString();

            CloudBlobContainer container = _blobClient.GetContainerReference(ulContainerName);
            CloudBlockBlob ulBlockBlob = container.GetBlockBlobReference(ulBlobName);

            CreateDummyDataAsync(_fileUploadSizeMB);

            byte[] byteArray = System.IO.File.ReadAllBytes(_filePath + "dummy_upload");

            ulBlockBlob.UploadFromStreamAsync(new MemoryStream(byteArray), _ulCancellationTokenSource.Token);
            _ulCancellationTokenSource.CancelAfter(5000); 

To anyone that ends up in this situation and can't get the cancellationToken to work... the workaround I eventually used was对于任何最终陷入这种情况并且无法让cancelToken工作的人......我最终使用的解决方法是

            BlobRequestOptions timeoutRequestOptions = new BlobRequestOptions()
        {
            // Allot 10 seconds for this API call, including retries
            MaximumExecutionTime = TimeSpan.FromSeconds(10)
        };

Then include the timeoutRequestOptions in the method arguments:然后在方法参数中包含 timeoutRequestOptions:

ulBlockBlob.UploadFromStreamAsync(new MemoryStream(byteArray), new AccessCondition(),
                                                   timeoutRequestOptions,
                                                   new OperationContext(),
                                                   new progressHandler(), cancellationToken.Token);

This will force the API call to timeout after a certain time.这将强制 API 调用在一定时间后超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM