asp.net-mvc – Parallel Blob上传间歇性抛出404 Bad Request

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – Parallel Blob上传间歇性抛出404 Bad Request前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个非常简单的服务,
public class AzureService : IAzureService
{
    private readonly CloudBlobContainer _container;
    public AzureService(ISettings settings)
    {
        var storageAccount = CloudStorageAccount.Parse(settings.BlobConnectionString);            
        var blobClient = storageAccount.CreateCloudBlobClient();
        _container = blobClient.GetContainerReference(settings.BlobContainerName);
    }

    public Task UploadBlobAsync(string fileName,Stream stream)
    {
        var blob = _container.GetBlockBlobReference(fileName);
        return blob.UploadFromStreamAsync(stream);
    }

    public Task DeleteBlobAsync(string fileName)
    {
        var blob = _container.GetBlockBlobReference(fileName);
        return blob.DeleteAsync();
    }
}

这个方法叫做,

public Task SaveAllAsync(Dictionary<string,Stream> images)
    {
        var tasks = new List<Task>();
        foreach (var image in images)
        {
            var fileName = image.Key;
            var stream = image.Value;
            var task = _azureService.UploadBlobAsync(fileName,stream);
            tasks.Add(task);
        }
        return Task.WhenAll(tasks);
    }

我的流是HttpPostedFileBase.InputStream.有时它工作,有时我得到远程服务器返回错误:(400)错误的请求..如果我放一个断点它也有效.

解决方法

我有同样的问题,我尝试上传20个图像,1次攻击,单线程工作,多线程使用await Task.WhenAll失败,“远程服务器返回错误:(400)错误请求.”

>请参阅上传[xxx]异步方法抛出的Microsoft.WindowsAzure.Storage.StorageException中的RequestInformation以获取更多详细信息.

>首先,RequestInformation说了一个关于MD5问题的错误代码“Md5Mismatch”,买了我的直觉说不然因为单线程就像一个魅力,然后……我发现它… DefaultBlobClient上的DefaultRequestOptions.ParallelOperationThreadCount对象和问题sovled.
> BlobRequestOptions Members MSDN

private CloudBlobContainer ConnectToImageContainer()
    {
        var credentials = new StorageCredentials(AccountName,ImagesContainerKey);
        var account = new CloudStorageAccount(credentials,useHttps: true);
        var client = account.CreateCloudBlobClient();
        client.DefaultRequestOptions.ParallelOperationThreadCount = 64; // max value
        client.DefaultRequestOptions.SingleBlobUploadThresholdInBytes = 67108864; // max value
        var container = client.GetContainerReference(ImagesContainerName);
        return container;
    }
原文链接:https://www.f2er.com/aspnet/247299.html

猜你在找的asp.Net相关文章