Search code examples
azureazure-storageazure-blob-storage

Reuse CloudBlobClient Object


I have these two objects for Azure Blob Storage access and want to use them in ASP.NET MVC application.

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("Deesd");

My question is: Can I reuse the same instance of the objects across all the application requests, or should I instantiate a new object in every method?


Solution

  • You can reuse instances, just don't access the same instance from multiple threads concurrently because it is not thread safe.

    UPDATE April 2019 (7yrs later)

    NOTE: You should always consult the latest SDK documentation.

    Yes, it is now (as of this update at least) safe to use CloudBlobClient and other objects in a thread safe manner in newer versions of the SDK. In fact, it is encouraged by some documentation that you find, but it is technically still not guaranteed to remain that way by design (e.g. future major versions of the SDK could reneg on this).

    As usual, you should probably be providing an abstraction for your application level logic that hides the client and its lifetime as much as possible. Then you let the abstraction worry about lifetime management. Maybe that's using a simple static instance today, it's maybe using pooling tomorrow, but at least if there's a problem the majority of your application logic is abstracted from it. 👍