I have an expensive server side resource ( which is thread safe ). What I want to provide is:
- a remote REST interface, where other apps can submit data and get it processed
From non-functional perspective:
- I want the expensive server side object to be Singleton, i.e., single instance will service all requests
- I want to limit the number of requests being entertained – if there are more requests then the allowed pool, rest should wait in queue to get processed.
I can write custom code to accomplish above said, but I do not want do so. Its been a while since I have used WCF – anyone who can guide me in designing the architecture?
Regards.
FYI: due to licensing limitations I really cannot create more then one object instances.
Why don’t you use the Object Pool pattern to control the lifetime of expensive server side objects?
The Singleton and imposing restrictions on the number of requests sounds risky. Sooner or later it will take revenge on you because the number of clients will grow and your service will be inefficient. Also, Singletons are risky in a concurrent environment and if the Singleton has its own state, two or more concurrent requests will blow it up.
On the other hand, you will have a complete control over your pool, how many expensive objects are created and when. And yes, you need a custom code.
If you still need something out of the box, you can control your service with the
ServiceBehaviorattribute. TheInstanceContextModeandConcurrencyModeattributes will probably work as you expect – allowing you to set the Singleton mode and queue incoming requests (InstanceContextMode.SingleandConcurrencyMode.Single).