Re: Implementing Singleton in ASP.Net
From: David Wang [Msft] (someone_at_online.microsoft.com)
Date: 10/21/05
- Previous message: Wei-Dong XU [MSFT]: "RE: Looking for an article on identities used in IIS 6.0 web applicati"
- In reply to: RaVs: "Implementing Singleton in ASP.Net"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Thu, 20 Oct 2005 22:51:28 -0700
Your question involves ASP.Net code, which instantly removes it from the
realm of IIS into the realm of ASP.Net . Not certain what your question has
to do with IIS Security...
Try:
microsoft.public.dotnet.framework.aspnet
www.asp.net Forums
I think you also want to read this blog entry to get an idea of how Session
State works (conceptually) because some of your terminology seems confused
(to me):
http://blogs.msdn.com/david.wang/archive/2005/09/19/Why_do_I_lose_ASP_Session_State_on_IIS6.aspx
Also, your implementation of GetCurrentSingleton() is flawed and has a race
condition. You need to obtain the lock PRIOR to examining the singleton
value, and release it when you are done examining/modifying its value.
Otherwise, two threads can simultaneously examine the singleton value prior
to obtaining the lock and both will succeed...
-- //David IIS http://blogs.msdn.com/David.Wang This posting is provided "AS IS" with no warranties, and confers no rights. // "RaVs" <RaVs@discussions.microsoft.com> wrote in message news:8D2EB2B6-D7BF-4D0E-B036-5165FAB3A6D7@microsoft.com... I was not sure where to post this question. This discussion group seemed most appropriate. If any one things otherwise can they please redirect me to the right discussion group? Here qoes the question anyway... I have built an ASP.net application that uses the singleton pattern to keep one instance of the user object for the life time of a user request. My problem arises when two or more users try to access the same singleton. Since ASP.net uses one process for the entire lifetime of the application and the threads are reused for request hence i implemented the singleton using the HttpContext Session. My beleive was that for every request from the user there would be one singleton object instantiated. If this is the case then why are the users getting crossed while browsing the application. The singleton code is a follows; ---------------------------------------------------------------------------- ---------------- private static object syncRoot = new object(); string SESSION_SINGLETON = "iPlan.Domain.iPlanUser"; public static iPlanUser GetCurrentSingleton() { iPlanUser single = null; if(HttpContext.Current.Session[SESSION_SINGLETON] == null) { lock(syncRoot) { iPlanUser single = new iPlanUser(); HttpContext.Current.Session[SESSION_SINGLETON] = single; } } else { single = (iPlanUser)HttpContext.Current.Session[SESSION_SINGLETON]; } return single; }//End Get Current Singleton public static void Dispose() { //Clean up this object so that the garbage collector (GC) can reclaim memory space System.Web.HttpContext.Current.Session.Remove(SESSION_SINGLETON); }//End Dispose ---------------------------------------------------------------------------- ---------------- Can anyone please help me understand this code a bit better or tell me what i am not doing correctly. Thanx RaVs
- Previous message: Wei-Dong XU [MSFT]: "RE: Looking for an article on identities used in IIS 6.0 web applicati"
- In reply to: RaVs: "Implementing Singleton in ASP.Net"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|