'How should BlobServiceClient be created?

Should the BlobServiceClient be created in a similar pattern like HttpClient, effectively as a singleton, or should it be done per request?

My instinct suggests that it should be a singleton but I couldn't really find anything suggesting this for definite. I've currently got some code like this:

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....
    }
}

My assumption is that this is the preferred thing to do, where the BlobServiceClient is created at this scope and my container client is created at the time I need it. Can anyone point to whether this is best practice or perhaps an anti pattern of some sort?



Solution 1:[1]

Looks like it's suggested for the Azure SDK clients v12 that they should be singletons. All instances are threadsafe apparently: https://devblogs.microsoft.com/azure-sdk/lifetime-management-and-thread-safety-guarantees-of-azure-sdk-net-clients/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sr28