6

If I am adding Windows Azure queue items or blob storage references from an MVC application using the .NET SDK, how long should each component stay about?

For queues we have CloudStorageAccount, CloudQueueClient and CloudQueue.

For blob storage, we have CloudStorageAccount, CloudBlobClient and CloudBlobContainer.

I am assuming that it would be best to create each component once per web request but don't really now how expensive it is to create each item. Again I am assuming that keeping the clients around between web requests using a singleton lifetime for example would not be a good plan but have nothing to go on.

Paul Hiles
  • 9,558
  • 7
  • 51
  • 76

1 Answers1

7

Those .NET objects are lightweight. None of them will allocate resources or perform network operations just by being created. It's generally not worth the effort to cache or pool them unless you create quite a lot of them.

We don't even bother to cache them on a per-requests basis - we just create them as needed and throw them away. Caching them may save you some pressure on the garbage collector, but I doubt it would be much.

Brian Reischl
  • 7,216
  • 2
  • 35
  • 46
  • 1
    How do you know these are lightweight... Is this documented anywhere? – John Jun 27 '15 at 03:26
  • Might be, but if so I couldn't tell you where off the top of my head. This was also a full major version ago, so it may not be true anymore. I would suggest taking a look at the source, since these are open source now. – Brian Reischl Jul 01 '15 at 02:34
  • In [asp.net core terms](https://docs.asp.net/en/latest/fundamentals/dependency-injection.html#service-lifetimes-and-registration-options), would you use AddTransient CloudStorageAccount? (Instead of singleton or scoped) – Ciantic Aug 04 '16 at 16:52
  • I'm not terribly familiar with ASP.Net core, but from a brief reading of those docs I think either transient or scoped would be reasonable. – Brian Reischl Aug 05 '16 at 18:20
  • Great answer, exactly what I was looking for and EXACTLY what SHOULD be in the docs. – pomeroy Oct 11 '17 at 03:00