Last post Mar 04, 2011 03:02 PM by atconway
Mar 04, 2011 06:57 AM|martin.gredin|LINK
I have a simple Asp.net web application that contains 2 web services. These web services uses a rather small amount of data stored in a database as input parameters for calculations performed in the web services. The database are only read, never added to
or altered. I have tried to instead place the data in List<T> objects placed in a singleton inside the web service but I am not sure it is very good for the performance of the web service. Initial tests with Silk Performer indicate that the response time from
the web services increase linearly with the amount of users who access them.
Is this behavior inherent for a singleton or any static variables in a website?
Is there a way to implement a data cache that initializes at startup and never discards any data more efficiently then with a singleton?
The singelton is implemented as following:
public class DataManager
private static DataManager instance = null;
private static readonly object padlock = new object();
public static DataManager Instance
if (instance == null)
if (instance == null)
instance = new DataManager();
public readonly List<dataObj> dataObjList;
Mar 04, 2011 07:19 AM|sudeep_13|LINK
Hi, You can use the caching mechanism in webservice. Use the [WebMethod(CacheDuration=60)] attribute to specify the cache duration.
Please refer these links
Mar 04, 2011 09:07 AM|LudovicoVan|LINK
IMO, your implementation is fine, with one only proviso, i.e. marking your property as readonly just makes the list reference readonly, not its contents. You should return a ReadOnlyCollection<T> instead, and for performance reasons the private member should
be a ReadOnlyCollection<T> already. Look up ReadOnlyCollection<T> on MSDN, you will find an example at the bottom. (Your property could actually be an IList<T>, as ReadOnlyCollection<T> implements IList<T>, still the private member should be a ReadOnlyCollection<T>.)
In particular, the response times you get are relative to the web services performance, and the impact of accessing your singleton and its property is negligible compared to that.
Mar 04, 2011 03:02 PM|atconway|LINK
I agree with the previous as well; there is nothing inherent of using a Singleton that has a performance degradation by increasing the acquisition of the singleton instance. There is an interesting write up in the link below which discussed the locking mechanism
when accessing a singleton and how little the overall logic had any affect on performance (with something like a billion times in less than 1 minute).
Implementing the Singleton Pattern in C#:
One possibility to improve performance if needed could be to migrate your .asmx web service to WCF. With WCF you can use other protocols besides HTTP like Net.TCP, and also include other options for hosting besides IIS (like Windows Services, etc) depending
on your needs. Point being, there are more communication, security, and hosting options which all when configured properly
could help with performance issues you might experience currently.