Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of static variables in ASP.NET sites

Tags:

c#

.net

asp.net

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.

like image 811
Erik A. Brandstadmoen Avatar asked Jun 30 '13 12:06

Erik A. Brandstadmoen


People also ask

What is the scope of static variable?

The scope of the static local variable will be the same as the automatic local variables, but its memory will be available throughout the program execution. When the function modifies the value of the static local variable during one function call, then it will remain the same even during the next function call.

Do static variables have global scope?

The difference between static variables and global variables lies in their scope. A static variable only has a block scope while a global variable can be accessed from anywhere inside the program.

What is the scope and longevity of static variables?

The space for the static variable is allocated only one time and this is used for the entirety of the program. Once this variable is declared, it exists till the program executes. So, the lifetime of a static variable is the lifetime of the program.

What is static variable in asp net?

Static variables are values that are also available across the whole application, but have some performance advantages and fewer overheads. The main difference to static variables and the previous examples is the fact that static variables are a side benefit of ASP.NET being object-oriented and the global being a .


2 Answers

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.

like image 189
James Avatar answered Oct 14 '22 19:10

James


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.

like image 20
Tamim Al Manaseer Avatar answered Oct 14 '22 20:10

Tamim Al Manaseer