【问题标题】:Azure Storage: container.CreateIfNotExistsAsync() exits app without Exception or success/failAzure 存储:container.CreateIfNotExistsAsync() 退出应用程序没有异常或成功/失败
【发布时间】:2021-03-19 05:47:01
【问题描述】:

我正在关注此 Ms doc Authorize access to blob and queue data with managed identities for Azure resources 以连接到 Azure blob。

string containerEndpoint = 
    string.Format("https://{0}.blob.core.windows.net/{1}", accountName, containerName);

BlobContainerClient containerClient = new BlobContainerClient(new Uri(containerEndpoint), new DefaultAzureCredential());
try
{
    var task = await containerClient.CreateIfNotExistsAsync(); // at this point app exist without exception. 
    string blobContents = "This is a block blob.";
    byte[] byteArray = Encoding.ASCII.GetBytes(blobContents);

    using (MemoryStream stream = new MemoryStream(byteArray))
    {
        await containerClient.UploadBlobAsync(blobName, stream);
    }
}
catch (RequestFailedException e)
{
    Console.WriteLine(e.Message);
    Console.ReadLine();
    throw;
}

正如containerClient.CreateIfNotExistsAsync() 评论应用程序退出时所指出的那样。

【问题讨论】:

  • 你只是在捕捉RequestFailedException。尝试为Exception 添加一个 catch 块,看看它抛出了什么
  • 这段代码在我这边没有问题。你使用什么版本的程序集?
  • 正如 Andrew 所提到的,捕获基本的 Exception 类型,这样我们就可以查看是否有任何东西被抛出。这将导致捕获任何类型的异常。

标签: c# .net azure-blob-storage azure-managed-identity


【解决方案1】:

刚刚遇到同样的问题。

我发现这是因为我的 async 函数不是 awaited。

async static Task CreateBlockBlobAsync()
{
    // ...
    var task = await containerClient.CreateIfNotExistsAsync();
    // ...
}

// Call the function like this would produce your behavior
CreateBlockBlobAsync();

// Instead, await that function and everything goes well
await CreateBlockBlobAsync();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2012-12-17
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多