I am referencing a (black box) dll which has extremely expensive initialization (takes 3-4 seconds). It is used by an asp.net application that has several hundred simultaneous users.
Because of the expensive initialization I cannot use it as an instance variable. My first thought is to store the instance in a static variable and use the c# lock() method to avoid race conditions. This works fine, but obviously the lock() method around a single static variable is going to be inefficient when multiple users want to access the library simultaneously.
I want to implement an object pool so that multiple instances of this library can be employed. I can't use COM+. What is the best way to implement an object pool in c#, .Net 4.5.1, when the pool will be consumed by an asp.net application?
There are many references to this MSDN article suggesting use of a ConcurrentBag, but many say this works best when a single thread is adding/removing items from the pool. In my case multiple threads will be adding/removing items, so this approach does not appear to be the right one for asp.net, but if I am wrong in stating that please correct me: https://msdn.microsoft.com/en-us/library/ff458671(v=vs.110).aspx
I found this answer very informative, but it is dated: C# Object Pooling Pattern implementation