【发布时间】: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