12.11.04

Singleton Web Service

One way of achieving singleton pattern in a Web Service is storing an object instance in Application object.

Another way is implementing singleton mechanism on your own:



Private constructor ensures that MyObject could not be instantiated directly and that MyObject instance could only be retrieved through Instance property. If all web service methods call MyObject’s methods instead of implementing their own functionality each WS session will work with the same instance (singleton).
In this implementation MyObject singleton instance will remain active until ASP.NET application times-out or process is recycled or IIS is restarted. If you need a longer living singleton you can implement the same as above in Windows Service and use Remoting for out-of-process communication or use Remoting’s own Singleton implementation.

No comments: