Re: Implementing Singleton in ASP.Net

From: David Wang [Msft] (someone_at_online.microsoft.com)
Date: 10/21/05

  • Next message: David Wang [Msft]: "Re: http logs in shared hosting environment"
    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
    

  • Next message: David Wang [Msft]: "Re: http logs in shared hosting environment"

    Relevant Pages

    • Singleton problem under IIS
      ... I have a remote object that is hosted by IIS. ... Basically i need a singleton ... ObjNumber always returns 1 to the client. ...
      (microsoft.public.dotnet.framework.remoting)
    • Re: How to kill a singleton Part II
      ... Sunny wrote: ... >> and remoting and IIS. ... >> I still can't find a satisfactory way to remove a Singleton from service. ... >> client will come along before I can stop the website.. ...
      (microsoft.public.dotnet.framework.remoting)
    • Re: Singleton and IIS
      ... Singletons and IIS ... >I'd like to build a .NET Remoting component that has to be singleton. ... >connected client. ... >I'm now wondering if i could host this .NET Remoting component in IIS. ...
      (microsoft.public.dotnet.framework.remoting)
    • Re: How to kill a singleton Part II
      ... > and remoting and IIS. ... > I still can't find a satisfactory way to remove a Singleton from service. ... > Alternatively you can stop IIS but that will kill all activity on ALL ... > client will come along before I can stop the website.. ...
      (microsoft.public.dotnet.framework.remoting)
    • How to kill a singleton Part II
      ... Further to my mail last week I've been playing a bit more with Singletons and remoting and IIS. ... I still can't find a satisfactory way to remove a Singleton from service. ... The Singleton registers a sponsor ... Every the sponsor is called to renew the lease ...
      (microsoft.public.dotnet.framework.remoting)