简体   繁体   中英

Scope of static variables in ASP.NET sites

If running multiple ASP.NET applications in the same application pool, how many instances will I have of a class' static variable?

  1. One per application pool?
  2. One per application pool worker process?
  3. One per application?
  4. Something else?

Just to give some context:

I'm thinking specifically of a ServiceLocator implementation we have that holds a UnityContainer in a static class variable. The question is, will multiple apps registering a container on the ServiceLocator interfere with one another?

The apps are running in IIS 7.5 on .NET 4.0, should that make any difference.

Example code (simplified)

public static class ServiceLocator
    {
        private static IUnityContainer _container;

        public static void Initialize(IUnityContainer container)
        {
            if (_container != null)
            {
                throw new ApplicationException("Initialize should only be called once!");
            }
            _container = container;
        }

    }

If i run this from two different web applications which run in the same application pool, , typically in Application_Start, will it throw an exception on the second invocation? Will it always throw an exception? Will it never throw an exception? Will it throw an exception in some configurations?

UPDATE: I know there will be one instance of the static variable per application domain. So, the question could be rephrased to "If running multiple ASP.NET applications in the same application pool, how many App Domains will I have?"

I've been looking a lot around, but haven't found any authoritative references on this. Any help is appreciated, preferably with references to official Microsoft documentation.

If running multiple ASP.NET applications in the same application pool, how many App Domains will I have?

Each application pool can have multiple worker processes, each worker process will run different application instances. Each instance of an application has a separate AppDomain - so the answer to your original question is one per application instance .

I know that every static variable lives for the lifetime of the App Domain.

So based on that, it will live per application pool process.

Based on the fact that there will be one instance of a static variable per AppDomain, and regarding this (almost 10 year old) article by K. Scott Allen, there is one AppDomain per ASP.NET application, I will conclude that there will be one instance of each shared variable per ASP.NET Web application, even though they all run in the same application pool.

If introducing more worker processes, I would suspect this to be one instance per application per process it's running in.

Even though the code for both of the applications resides inside the same process, the unit of isolation is the .NET AppDomain. If there are classes with shared or static members, and those classes exist in both applications, each AppDomain will have it's own copy of the static fields – the data is not shared.

( http://odetocode.com/Articles/305.aspx , see the section "AppDomains and you").

So, the answer to my original question would be 3), if running one worker process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM