Global.aspx: variable instantiation

Last post 10-13-2005 9:05 AM by Kumar Reddi. 5 replies.

Sort Posts:

  • Global.aspx: variable instantiation

    10-12-2005, 10:49 AM
    • Member
      15 point Member
    • raven
    • Member since 10-12-2005, 2:38 PM
    • Posts 3

    Hi,

    I'm kind of new to ASP.net and im not sure how to go about this. I have a class that loads a dictionary, and I want it to be loaded just once per lifecycle. I want to instantiate the class in Global.aspx's Application_Start() function, but the class is declared in a different aspx file. I don’t want to have it as a shared (static) variable, as it would create issues with multiple sessions... Any suggestion would be appreciated.

    Thanks
    raven

     

  • Re: Global.aspx: variable instantiation

    10-12-2005, 1:00 PM
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,344

    The static/shared variable is appropriate when its setup during Application_Start. In fact, that's a best practices approach for setting up globals in a web app. Such a variable will be available to all sessions and is created before any sessions. In effect, you are guaranteed that there are no threading issues while Application_Start executes.

    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: Global.aspx: variable instantiation

    10-12-2005, 6:44 PM
    • Member
      15 point Member
    • raven
    • Member since 10-12-2005, 2:38 PM
    • Posts 3
    Peter, could you please clear one thing for me, please? Suppose the first session is running and changes are being made to the shared dictionary, and while its running a second session is started, what would happen to the shared variable? would the second session start with a fresh dictionary? or would it have the dictionary what was being changed by the first session? Im a bit confused about this...

    Thanks
    raven

  • Re: Global.aspx: variable instantiation

    10-12-2005, 8:44 PM
    • Star
      12,874 point Star
    • Kumar Reddi
    • Member since 11-11-2004, 4:54 PM
    • Virginia
    • Posts 2,357
     raven wrote:
    Suppose the first session is running and changes are being made to the shared dictionary, and while its running a second session is started, what would happen to the shared variable? would the second session start with a fresh dictionary? or would it have the dictionary what was being changed by the first session?


    it would  have the dictionary what was being changed by the first session
    Kumar Reddi
  • Re: Global.aspx: variable instantiation

    10-13-2005, 7:48 AM
    • Member
      15 point Member
    • raven
    • Member since 10-12-2005, 2:38 PM
    • Posts 3
    Is there any other alternative to having each session start with the same old dictionary? which brings be back to my original question of how could I have the deceleration of the dictionary in a class other than Global.aspx, and instantiate it in the Global.aspx without having to declare it as shared?

    Just out of curiosity how would asp.net react when both session would be modifing the dictionary at the same time?
  • Re: Global.aspx: variable instantiation

    10-13-2005, 9:05 AM
    • Star
      12,874 point Star
    • Kumar Reddi
    • Member since 11-11-2004, 4:54 PM
    • Virginia
    • Posts 2,357
    If you want each user session to have it own exclusive copy of an object, you should use it as a session object, not as an application state object. Application state is accessible by all the user sessions, so one session can modify and it would be modified forever for all the other session..

    So, try setting whatever as session variable in  the Session_Start event. Then it would be specific that user session only

     raven wrote:

    Just out of curiosity how would asp.net react when both session would be modifing the dictionary at the same time?


    Application State objects are thread safe. Asp.net will take care of the synchronization issues and would not allow two sessions to modify an application object at once   
    Kumar Reddi
Page 1 of 1 (6 items)