Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 905
sr28 Avatar asked Feb 27 '26 00:02

sr28


1 Answers

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/

like image 133
sr28 Avatar answered Feb 28 '26 12:02

sr28



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!