在ASP.NET网站静态变量的作用域 [英] Scope of static variables in ASP.NET sites

查看:101
本文介绍了在ASP.NET网站静态变量的作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果运行在同一个应用程序池,我多少实例将有一个类的多个ASP.NET应用程序的静态变量?

  1. 在一个每个应用程序池?
  2. 在一个每个应用程序池工作进程?
  3. 在每一个应用程序?
  4. 在其他什么东西?

只给一些方面:

我明确的服务定位的实现想我们有一个持有UnityContainer在一个静态类变量。现在的问题是,将在服务定位注册一个容器多个应用程序互相干扰?

该应用程序是在.NET 4.0运行在IIS 7.5,应该说任何区别。

例如code(简体)

 公共静态类的服务定位
    {
        私有静态IUnityContainer _container;

        公共静态无效初始化(IUnityContainer容器)
        {
            如果(_container!= NULL)
            {
                抛出新ApplicationException的(初始化应该只被调用一次!);
            }
            _container =容器;
        }

    }
 

如果我跑这从它在同一个应用程序池中运行两个不同的Web应用程序,通常是在的Application_Start,将它扔在第二次调用一个例外?将它总是抛出一个异常?它会不会抛出一个异常?它会在某些配置中抛出一个异常?

更新:我知道一定会有每个应用程序域静态变量的一个实例。因此,问题的措词,如果在同一个应用程序池中运行多个ASP.NET应用程序中,有多少应用程序域我有吗?

我一直在寻找了很多周围,但没有发现有这方面的权威参考。任何帮助是pferably pciated AP $ P $,$ P $与参考微软官方文档。

解决方案
  

如果运行在同一个应用程序池的多个ASP.NET应用程序,应用程序域将有多少我有?

每个应用程序池可以有多个工作进程,每个工作进程将运行不同的应用程序实例。每个实例的应用程序都有一个单独的的AppDomain - 这样的回答你原来的问题是每个应用程序的实例之一。

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.

这篇关于在ASP.NET网站静态变量的作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆