C# Azure在上载文件时,引发异常";指定的blob不存在";

C# Azure在上载文件时,引发异常";指定的blob不存在";,c#,azure,asp.net-core,azure-storage,azure-storage-blobs,C#,Azure,Asp.net Core,Azure Storage,Azure Storage Blobs,我通过.NETCore开发了一个应用程序。我使用Azure存储来存储我的文件。但有时代码会抛出一个期望:指定的blob不存在 以下是我启动文件上载过程的方式: entity = FileUploadBusinessRule.UploadFile(entity, companyInformationEntityRequestDto.BusinessLicenseBase64, "BusinessLicenseUrl"); entity = Fi

我通过.NETCore开发了一个应用程序。我使用Azure存储来存储我的文件。但有时代码会抛出一个期望:指定的blob不存在

以下是我启动文件上载过程的方式:

entity = FileUploadBusinessRule.UploadFile(entity, companyInformationEntityRequestDto.BusinessLicenseBase64,
                "BusinessLicenseUrl");

entity = FileUploadBusinessRule.UploadFile(entity, companyInformationEntityRequestDto.CompanyLogoBase64,
                "CompanyLogoUrl");
FileUploadBusinessRule.UploadFile:

public static class FileUploadBusinessRule
{
    public static T UploadFile<T>(T entity, string file, string propName)
    {
        IStorageService _storageService = DependencyContainerHelper.Container.Resolve<IStorageService>();

        Type t = entity.GetType();
        var propertyValue = t?.GetProperty(propName)?.GetValue(entity)?.ToString();


        if (!file.IsNullOrEmpty())
        {
            if (file.Contains("http://") || file.Contains("https://"))
                return entity;

            if (!propertyValue.IsNullOrEmpty())
                _storageService.DeleteBlobData(propertyValue);

            t?.GetProperty(propName)?.SetValue(entity, _storageService.UploadFileByBase64String(file));
        }
        else
        {
            if (!propertyValue.IsNullOrEmpty())
                _storageService.DeleteBlobData(propertyValue);
        }

        return entity;
    }
}
public string UploadFile(string strFileName, byte[] fileData, string fileMimeType)
    {
        try
        {
            var _task = Task.Run(() => this.UploadFileToBlobAsync(strFileName, fileData, fileMimeType));
            _task.Wait();
            string fileUrl = _task.Result;
            return fileUrl;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }
上载文件:

public static class FileUploadBusinessRule
{
    public static T UploadFile<T>(T entity, string file, string propName)
    {
        IStorageService _storageService = DependencyContainerHelper.Container.Resolve<IStorageService>();

        Type t = entity.GetType();
        var propertyValue = t?.GetProperty(propName)?.GetValue(entity)?.ToString();


        if (!file.IsNullOrEmpty())
        {
            if (file.Contains("http://") || file.Contains("https://"))
                return entity;

            if (!propertyValue.IsNullOrEmpty())
                _storageService.DeleteBlobData(propertyValue);

            t?.GetProperty(propName)?.SetValue(entity, _storageService.UploadFileByBase64String(file));
        }
        else
        {
            if (!propertyValue.IsNullOrEmpty())
                _storageService.DeleteBlobData(propertyValue);
        }

        return entity;
    }
}
public string UploadFile(string strFileName, byte[] fileData, string fileMimeType)
    {
        try
        {
            var _task = Task.Run(() => this.UploadFileToBlobAsync(strFileName, fileData, fileMimeType));
            _task.Wait();
            string fileUrl = _task.Result;
            return fileUrl;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }
UploadFileToBlobAsync:

private async Task<string> UploadFileToBlobAsync(string strFileName, byte[] fileData, string fileMimeType)
    {
        try
        {
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(accessKey);
            CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
            string strContainerName = "files";
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(strContainerName);
            string fileName = this.GenerateFileName(strFileName);
            if (await cloudBlobContainer.CreateIfNotExistsAsync())
            {
                await cloudBlobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
            }

            if (fileName != null && fileData != null)
            {
                CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
                cloudBlockBlob.Properties.ContentType = fileMimeType;
                await cloudBlockBlob.UploadFromByteArrayAsync(fileData, 0, fileData.Length);
                return cloudBlockBlob.Uri.AbsoluteUri;
            }
            return "";
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我尝试了相同的文件,但还是抛出了一个异常。此外,即使我包装了try-catch块,应用程序也会停止。

指定的blob不存在
您可以尝试使用fiddler etc工具捕获请求并检查哪个请求导致404错误。检查是否有其他应用程序正在删除它?当您说
wait wait cloudBlobContainer时。CreateIfNotExistsAsync()
正在引发异常,例外情况是什么?它不可能是“blob不存在”,因为您正在执行容器操作!旁注:“即使我包装了try-catch块,应用程序也会停止”-看着你的try-catch块,你只是在重新拉伸它,这和根本没有它一样好(更糟糕的是,因为你正在做
抛出ex
,这会丢失上层的堆栈跟踪)!当然没有帮助:)另一方面注意:为什么要混合异步和非异步代码?在UploadFile方法中,通过Task.Run和Wait调用异步代码的方式有点古怪。您应该始终使用async,或者根本不使用async(尽管没有太多理由不使用async)。您可以阅读有关异步的更多信息