static constructors not always running when debugging ASP.NET sites.

Discussion in 'ASP .Net' started by Peter Rilling, Nov 12, 2004.

  1. Here is a weird behavior that I would not have expected in VS.NET during
    debugging. Maybe someone has some insight as to why this is happening.

    1) I have a VS.NET web project. This contains a class with a static
    constructor.
    2) The static constructor loads custom information from the web.config.
    3) First time I debug, everything works fine. I hit the breakpoint in the
    static constructor.
    4) I stop debugging. At this point I would have expected all objects to be
    removed from memory.
    5) I start debugging again. This time I do not hit the static constructor.
    6) I make a change in the web.config file.
    7) I start debugging again. This time the static constructor does run.

    Why might this be happening? I would have thought that each time I run the
    debugger, it would be starting a new process which would have new variables.
    Static variables I thought were confined to the process in which it is
    running in. Another related question might be, what is the scope and
    lifetime of a static object in an ASP.NET world?
     
    Peter Rilling, Nov 12, 2004
    #1
    1. Advertising

  2. Peter Rilling

    Scott Allen Guest

    Hi Peter:

    Are you using attach / detach process during debugging?

    Changing web.config will definitely force the web application to
    reload and re-execute static ctors.

    Static variables are scoped to an application domain. There can be
    more than one app domain in a process, which is how asp.net can host
    mutliple web applications inside a single worker process - each app
    gets it's own app domain, and the statics do not interfere with each
    other. A static ctor is guaranteed to run no more than once for an app
    domain.

    If you keep a reference to an object in a static field it will be
    there for the life of the app domain (unless you overwrite or null out
    the field).

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Fri, 12 Nov 2004 14:44:11 -0800, "Peter Rilling"
    <> wrote:

    >Here is a weird behavior that I would not have expected in VS.NET during
    >debugging. Maybe someone has some insight as to why this is happening.
    >
    >1) I have a VS.NET web project. This contains a class with a static
    >constructor.
    >2) The static constructor loads custom information from the web.config.
    >3) First time I debug, everything works fine. I hit the breakpoint in the
    >static constructor.
    >4) I stop debugging. At this point I would have expected all objects to be
    >removed from memory.
    >5) I start debugging again. This time I do not hit the static constructor.
    >6) I make a change in the web.config file.
    >7) I start debugging again. This time the static constructor does run.
    >
    >Why might this be happening? I would have thought that each time I run the
    >debugger, it would be starting a new process which would have new variables.
    >Static variables I thought were confined to the process in which it is
    >running in. Another related question might be, what is the scope and
    >lifetime of a static object in an ASP.NET world?
    >
    >
     
    Scott Allen, Nov 13, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,301
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    589
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    608
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    396
    Juha Nieminen
    Sep 19, 2008
  5. Yitzak

    2 sites or not 2 sites

    Yitzak, Mar 7, 2009, in forum: ASP .Net
    Replies:
    5
    Views:
    438
    Andrew Morton
    Mar 10, 2009
Loading...

Share This Page