Sunday, February 13, 2011

The effect of static properties in a web context

I need to change a static property on an object in our web application. The property has a default value that is hard-coded into the object. If I change the static property in my Application_Start does that change stick:

A) Forever (well, until the app is recycled)

B) Until the object is GC'd then re-inialised by the next accessor

C) Depends

Note that the property I'd be setting is just a String

  • The scope of a static variable is its AppDomain. So no, it won't get garbage collected - but if the AppDomain is recycled (which can happen a fair amount in ASP.NET) then you'll end up with a "new" static variable, effectively.

    From Jon Skeet
  • In my experience with our web apps here, the answer is A. As far as I know, a static class will never be GCed, it lives on for the life of the process (in this case, the ASP.NET worker process)

    Jon Skeet : Life of the AppDomain, not process.
    From Ch00k
  • Go for A) App Instance Variable

    For context storage, refer to httpContext.

    From codemeit

0 comments:

Post a Comment