BlobServiceClient应该如何创建?

sr2*_*r28 7 c# azure azure-blob-storage

BlobServiceClient 是否应该以类似于 HttpClient 的模式创建,有效地作为单例,还是应该根据请求完成?

我的直觉表明它应该是一个单身人士,但我找不到任何明确的建议。我目前有一些这样的代码:

public class MyAzureThing
{
    private readonly BlobServiceClient blobServiceClient;

    public MyAzureThing(Uri baseUri)
    {
        blobServiceClient = new BlobServiceClient(baseUri, new DefaultAzureCredential());
    }

    public async Task CreateContainerAsync(string name)
    {
        var containerClient = blobServiceClient.GetBlobContainerClient(name);
        
        // other logic....
    }
}
Run Code Online (Sandbox Code Playgroud)

我的假设是这是首选的做法,其中 BlobServiceClient 是在此范围内创建的,而我的容器客户端是在我需要时创建的。谁能指出这是否是最佳实践,或者可能是某种反模式?

sr2*_*r28 7

看起来建议 Azure SDK 客户端 v12 它们应该是单例。所有实例显然都是线程安全的: https: //devblogs.microsoft.com/azure-sdk/lifetime-management-and-thread-safety-guarantees-of-azure-sdk-net-clients/