14

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.

Erik A. Brandstadmoen
  • 10,430
  • 2
  • 37
  • 55

3 Answers3

17

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.

James
  • 80,725
  • 18
  • 167
  • 237
  • 1
    And you just beat me to it, and after all you edits the last three minutes, your answer is correct, so I'll give you the credit ;) Actually, it's One Per application per worker process, isn't it? If one application runs in multiple worker precesses? – Erik A. Brandstadmoen Jun 30 '13 at 13:04
  • @ErikA.Brandstadmoen No it's still per instance, if an application has multiple worker threads those should still share the same `AppDomain`. – James Jun 30 '13 at 13:10
  • 1
    Multiple worker processes (Web Garden), not multiple worker threads. And, yes, one per instance is still correct, because you would have one instance of an application per process. So, "One per application instance" is precise, and you would have one application instance per worker process, correct? – Erik A. Brandstadmoen Jun 30 '13 at 13:13
  • @ErikA.Brandstadmoen your previous comment was referring to *threads*, not *processes* - 2 different things. I have already stated in my answer that each *process* hosts different app *instances*. Yes, you would have 1 instance per process. I have updated my answer for clarity. – James Jun 30 '13 at 13:15
  • The comment was referring to threads before I edited it to refer to processes, yes. Now it refers to processes ;) No matter, I think your answer as it stands now is clear and correct. Just for the people coming after us... Thanks. – Erik A. Brandstadmoen Jun 30 '13 at 13:18
  • Two articles on this subject I found very enlightening. "The process (w3wp.exe) contains multiple application domains, typically a shared domain, a default domain, a system domain and one application domain per web application (virtual directory marked as application)." https://blogs.msdn.microsoft.com/tess/2008/08/19/questions-on-application-domains-application-pools-and-unhandled-exceptions/ Curiously both suggests you can have more than one AppDomain per process for but for different apps. https://www.codeproject.com/Articles/121096/Web-Server-and-ASP-NET-Application-Life-Cycle-in-D – Rohan Oct 28 '19 at 01:37
1

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.

Tamim Al Manaseer
  • 3,554
  • 3
  • 24
  • 33
  • So, to follow up, how many app domains will I have when running ten applications in one application pool? Please specify using alternative 1-4 above, and please give a reference. – Erik A. Brandstadmoen Jun 30 '13 at 12:29
  • You also contradict yourself. In the first sentence, you say that I will have one per AppDomain, in the second, you say I will have one per process. I will definitely have more than one AppDomain per perocess. – Erik A. Brandstadmoen Jun 30 '13 at 12:56
  • It seems I misread ur question: but to be consistent: You'll have an instance of your static variable per App Domain, if your application runs in multiple app domains it will have multiple instances. Here is a nice read for you, http://msdn.microsoft.com/en-us/magazine/cc163791.aspx#S14 – Tamim Al Manaseer Jun 30 '13 at 13:56
1

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.

Erik A. Brandstadmoen
  • 10,430
  • 2
  • 37
  • 55