Reuse CloudBlobClient Object
Asked Answered
T

4

10

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?

Teeny answered 29/3, 2012 at 21:52 Comment(0)
A
12

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. πŸ‘

Akkad answered 29/3, 2012 at 22:22 Comment(7)
The answer is not to reuse CloudBlobClient. social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/… – Teeny
Reusing and using concurrently are two diff things, hence the explicit distinction in my answer. – Akkad
I continue to believe my answer is correct, though yes, the documentation general warns you away from this. – Australoid
I don't buy this accepted answer. I would do more research before reading this answer and end up creating thousands of clients when you don't really need to. Read simon's answer below. – Showplace
I agree with Andy. Concurrent access to the client is no problem ... its designed for such access - this answer is incorrect in saying dont - its actually advisable (see my answer below) – Zouave
Hey everyone! Look at the date on this answer and think about what version of the SDK was around back then and how many versions there have been since then and think about what kind of changes might have happened. Yes, I agree, the SDK is thread safe now, they've gone to great lengths to ensure this. Even the guidance that @SimonDowdeswell refers to was only put out last year... 6 years after this answer. I'll revise my answer. – Akkad
Some link that points out where you found this information would be nice, i checked learn.microsoft.com/en-us/dotnet/api/… and it says nothing about it! – Nowt
A
4

You can reuse it. To my knowledge, it contains no state whatsoever beyond what it's initialized with.

Australoid answered 29/3, 2012 at 22:19 Comment(2)
The answer is not to reuse CloudBlobClient. social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/… – Teeny
Yes, I understand your concern... I am not convinced either. The problem is that MS spits out too much poorly documented and barely working code which is obsolete upon launch. – Teeny
Z
3

This link categorically states that you should reuse the same client across requests because these clients manage connections and are thus capable of optimising the underlying calls to establish a connection with the service.

This is the direct link to Microsoft Documentation says the same

Cosmos Db advice is similar in that it recommends a singleton pattern for the lifetime of your application

Zouave answered 20/3, 2019 at 23:18 Comment(2)
what about multiple threads concurrently? I am assuming that is a yes. It's too bad the accepted answer says the opposite. – Showplace
multiple threads concurrently - that is a yes. Once the client is created accessing it from multiple threads is no problem (since they are not modifying it) – Zouave
U
1

Depends on the client version, I found an answer from someone at Microsoft here: https://github.com/Azure/azure-storage-net/issues/732#issuecomment-745749919

As for making CloudBlobClient singleton. One can attempt to do this with some success. However, please be mindful that V11 and prior types like CloudBlobClient and CloudBlob are stateful. They might have mutable properties and some APIs might mutate the state. Therefore I recommend caution while attempting this. I strongly recommend to move to V12, where all clients has been designed stateless and can be safely used with any lifespan.

Uprear answered 21/2, 2021 at 17:24 Comment(1)
+1 for V12 being safe to re-use - devblogs.microsoft.com/azure-sdk/… – Consolidate

© 2022 - 2024 β€” McMap. All rights reserved.