Reputation: 5106
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?
Upvotes: 11
Views: 11800
Reputation: 5106
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/
Upvotes: 10