24

Should I create CloudStorageAccount and CloudBlobClient instances every time I want to get blob from storage? For example, I implemented custom Virtual Path Provider for working with a blob storage. What is the best design solution: creation CloudStorageAccount and CloudBlobClient instances once as private fields of my custom Virtual Path Provider, or using of an utility with static (shared) methods that creates CloudStorageAccount and CloudBlobClient instances every time I want to get a blob from a storage? How expensive it from the point of performance?

eternity
  • 1,668
  • 15
  • 25
  • 1
    please see http://stackoverflow.com/a/9934393/468244 ... because the creation is not "free" and reuse normally is not a problem you should probably prefer reuse. – Simon Opelt May 30 '12 at 09:52
  • Ok. Thank you. But do these instances thread safe in context of VirtualPathProvider? As I understood there is only one instance of VirtualPathProvider per application (if I create a new instance and register it in global.asax on application_start), so there should not be any issue with a thread-safety? – eternity May 30 '12 at 10:18
  • as @sandrino stated there should be no issues with threads. Depending on your application it might also be necessary to keep `CloudBlobContainer`s after `CreateIfNotExist`ing them. But that depends on the use-case. – Simon Opelt May 30 '12 at 10:38

1 Answers1

21

You can reuse the CloudStorageAccount and CloudBlobClient since they hold no state (see Steve Marx's reply in Simon's link). The SDK is open source and you can simply look at the source on GitHub.

If you take a look at the CloudStorageAccount class you can see that its main purpose is to make sure you end up with StorageCredentials with a Blob/Queue and TableEndpoint. Looking at the constructor of the CloudBlobClient you can see that it stores the storage credentials, the endpoint Uri and some default values.

There isn't anything complicated here and there isn't any network I/O meaning the processing is very cheap. But when you think about performance every little optimization can help, so you could safely store it in a static variable (this might impact your unit testing) or if you use a IoC container you could choose to use a single instance for the whole container.

Now you need to take something into account. Both your CloudStorageAccount and your CloudBlobClient will save the storage credentials and the endpoint Uri. But what if you change this information through the portal (you maybe changed the key of your storage account)? If you store a single instance of the CloudStorageAccount/CloudBlobClient you might need to handle the RoleEnvironment.Changing event to 'refresh' these objects with the new storage account information.

Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
  • If this is the case then why I am getting error, http://stackoverflow.com/questions/24229288/parallel-blob-upload-throwing-404-bad-request-intermittently – Imran Qadir Baksh - Baloch Jun 15 '14 at 12:57
  • 3
    Don't reuse the CloudBlobClient, as it is not thread-safe (I actually found out the hard way where streams were written to the wrong Blob). I think you can reuse the CloudStorageAccount and just create a new CloudBlobClient each time. – dionoid Apr 04 '18 at 12:49