Implementing Singleton in ASP.Net
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Web Servers reviews > IIS server support > IIS Server Security > Implementing Singleton in ASP.Net




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Implementing Singleton in ASP.Net  
RaVs


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-24-05 04:03 PM

I was not sure where to post this question. This discussion group seemed mos
t
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 th
e
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







[ Post a follow-up to this message ]



    Re: Implementing Singleton in ASP.Net  
David Wang [Msft]


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-24-05 04:03 PM

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/ar...
e_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








[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:51 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register