I'm using the new WindowsAzure.Storage 2.0 (might not be a revelant information), and I'm implementing data access using CloudTableClient. Most samples I've seen are instanciating a CloudTableClient in the ctor of an ASP MVC Controller (instanciated per web request). Is there any performance penalty doing so? Would it be wise to keep a long running instance in a singleton style?
You need to create a new instance of CloudTableClient with each request. Instance members are not thread-safe, so you can't share a singleton.
I came across this question / answer wondering the same thing and whilst looking through the SDK's source code (for something else) I came across something useful:
When executing an operation, the SDK is using a HttpClientFactory, so a single static instance of HttpClient is being reused. Which is good, and corrects the Improper Instantiation antipattern, so the common reason to use a singleton is already sorted for us.
Relevant code can be found on github
During Execution
and the HttpClient factory is implemented via a static Lazy<T>
Notably the Storage SDK doesn't support Table anymore (instead the Cosmos SDK appears to provide it - I'm learning more), so this is likely a moot observation.
I think in 2020 you should reuse.
https://azure.microsoft.com/en-us/blog/performance-tips-for-azure-documentdb-part-1-2/
Looking at CloudTableClient documentation, I realize it's not thread safe.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Hence it is probably required to instanciate it for each request.
© 2022 - 2024 — McMap. All rights reserved.