Dev_Eric

新浪微博:卖螃蟹的程序员

导航

理解AppDomain和AppPool

应用程序池:
这是微软的一个全新概念:应用程序池是将一个或多个应用程序链接到一个或多个工作进程集合的配置。因为应用程序池中的应用程序与其他应用程序被工作进程边界分隔,所以某个应用程序池中的应用程序不会受到其他应用程序池中应用程序所产生的问题的影响。 
 
 应用程序域:

大家都知道,.net写的程序,都是托管的,何为托管?就是让“其他的程序”来管理,也解析运行,什么又在这里充当“其他程序”呢?这里大体上说是CLR(通用语言运行时),这只是大体上的,准确的在底层是怎么去处理托管程序与操作系统间的关系呢?

操作系统上运行的都是进程,这进程是非托管的。现在。我们有一个Demo.exe,他是用.net写的(这里与语言无关了,因为编设成程序集后,都成为IL语言了),当然是一个托管理程序。这里的问题就是怎么把Demo.exe变成一个进程,运行在操作系统的进程中。这里就引出了应用程序域(Application Domain),应用程序域(Application Domain)是“托管理代码与非托管理代码之间的桥梁”(引自《.NET组件编程设计》)

进程,应用程序域,.net程序集(这里是Demo.exe),之间的关系可以见下图:

  

(图1)
一个进程中可以有多个应用程序域(Application Domain),一个应用程序域(Application Domain)中可以有多个程序集。

应用程序域(Application Domain)的引入的好处在于,如果一个程序集出现错误,不会影响到别的应用程序域(Application Domain),同时他们又是一个进程中的。

在.net中,应用程序域(Application Domain)是用AppDomain类来表示的。

AppDomain CurrentAD=AppDomain.CurrentDomain;

上面的代码实现了获取当有程序集所在的应用程序域(Application Domain)。获取当前应用程序域(Application Domain)还可以通过当前线程来得到,如下:

AppDomain CurrentAD=Threed.GetDomain();

下面再看一下在当前应用程序域(Application Domain)中创建对象:

类:

Class Class1

{

   Public void FF()

{

  //实现功能

}

{

AppDomain CurrentAD=Threed.GetDomain();

Class1 C1=(Class1)CurrentAD.CreateInstanceAndUnwrap(“程序集名称”,”名命空间.类名”);

C1.FF();

这个是关于当前的应用程序域(Application Domain)的操作,怎么创建一个应用程序域(Application Domain)呢?看下面

AppDomain MyAppDomain=AppDomain.CreatDomain(“MyNewAD”);

Class1 C1=(Class1)MyAppDomain.CreateInstanceAndUnwrap(“程序集名称”,”名命空间.类名”);

C1.FF();

AppDomain MyAppDomain=AppDomain.CreatDomain(“MyNewAD”);

IObjectHandle handle=MyAppDomain.CreateInstance “程序集名称”,”名命空间.类名”);

Class1 C1=(Class1)handle.Unwrap();

C1.FF();

后都的好处在于用C1这个对象的时候才进处理。

 

 
First of all, Application Pool is a concept in IIS, but AppDomain is a concept in .NET.  You can write you own program to use 2 or more AppDomaines.

首先,应用程序池是IIS中的概念,而应用程序域是.net中的概念。你可以写一段程序运行在2个或更多的应用程序域上。

I did a test with the IIS7 Asp.net 2.0 on my Vista computer.

我在我的Vista电脑IIS7 Aps.net2.0上做了个测试。

 
Here is the test code:

下面是测试代码:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string info;
        info = "Current Process Name:" + Process.GetCurrentProcess().ProcessName + "
";
        info += "Current Process Id:" + Process.GetCurrentProcess().Id + "
";
        info += "Current Application Domain:"+AppDomain.CurrentDomain.FriendlyName + "
";
        info += "Current Application Domain Base Dir:"+AppDomain.CurrentDomain.BaseDirectory + "
";
        divInfo.InnerHtml = info;
    }
}
 
Now, I created 2 application pools called AppPool1 and AppPool2;

现在,我建立2个应用程序池AppPool1和AppPool2;

Then I created 3 applications called AppTest1, AppTest2 and AppTest3. All of them point to the same directory where my sample is.
I put AppTest1 under AppPool1, AppTest2 and AppTest2 under AppPool2.
然后我建立3个站点:AppTest1 应用程序池为AppPool1.AppTest2和AppTest3 应用程序池为AppPool2.都指向刚新建站点目录。
Here is the result:

下面是返回结果:

http://localhost/AppTest1/Default.aspx
Current Process Name:w3wp
Current Process Id:3784
Current Application Domain:/LM/W3SVC/1/ROOT/AppTest1-2-128701111683637820
Current Application Domain Base Dir:C:\inetpub\wwwroot\AppTest\

http://localhost/AppTest2/Default.aspx
Current Process Name:w3wp
Current Process Id:5044
Current Application Domain:/LM/W3SVC/1/ROOT/AppTest2-1-128701111868733395
Current Application Domain Base Dir:C:\inetpub\wwwroot\AppTest\

http://localhost/AppTest3/Default.aspx
Current Process Name:w3wp
Current Process Id:5044
Current Application Domain:/LM/W3SVC/1/ROOT/AppTest3-2-128701113026462030
Current Application Domain Base Dir:C:\inetpub\wwwroot\AppTest\
 
Here is the conclusion:
下面是结论:
 
IIS process is w3wp;
IIS进程都是w3wp;
Every application pool in IIS use it"s own process; AppPool1 uses process 3784, AppPool2 uses process 5044
每个IIS应用程序池都有自己的进程AppPool1 用进程3784.AppPool2用进程5044
Different applications in Asp.net will use different AppDomain;
不同的Asp.net站点用不同的应用程序域;
AppTest2 and AppTest2 are in different AppDomain, but in the same process.
站点AppTest2和AppTest3在不同的应用程序域,但在相同的进程中。
 
What"s the point to use them?
使用的关键在哪? 

Application pool and AppDomain , both of them can provide isolations, but use different approches. Application pool use the process to isolate the applications which works without .NET.  But AppDomain is another isolation methods provided by .NET.
应用程序池和应用程序域都可以提供程序隔离,但用途不一样。应用程序池在没有.Net环境下也可以提供程序间的隔离。而应用程序域是.Net提供的隔离方式。
If your server host thousands of web sites, you wont use thousands of the application pool to isolate the web sites, just becuase, too many processes running will kill the os. However, sometime you need application pool. One of the advantages for application pool is that you can config the identity for application pool. Also you have more flexible options to recyle the application pool. At least right now, IIS didnt provide explicit options to recyle the appdomain.

如果你的服务器有上千个站点,你不会采用上千个应用程序池去隔离站点,那是因为,在服务器中开启了太多的进程。尽管如此,有时你需要应用程序池。应用程序池的好处在于你可以配置应用程序池的标识。同样你可以更多灵活的方式去回收应用程序池。而IIS没有提供配置参数去回收应用程序池。

 

 

 

A question was asked on a forum that I frequent which I thought was worth writting a blog about.

Q: What is the difference between an application and an Appdomain?  I understand from my research so far that an Appdomain is a container within which ASPX runs and that Apppool is a process that starts the w3wp.exe worker process within which ASP applications run.

A: That's a good question.  Here are some key differences:

    • An application is an IIS term, but it's one that ASP.NET utilizes.  Essentially it creates a sandbox, or a set of boundaries to separate different sites, or parts of sites, from the others.
    • An AppDomain is a .NET term.  (In IIS7, AppDomains play a larger role within IIS, but for the most part it's an ASP.NET term)
    • An AppDomain contains InProc session state (the default session state mode).  So if an AppDomain is killed/recycled, all of your session state information will be lost. (if you are using the default InProc session state)
    • Applications can have multiple AppDomains in them although often times there is a one-to-one relationship between them.
    • In IIS6 and greater, there is the option of creating groups, or "pools" of applications that can be bundled together or separated; however the server administer decides.  These are called Application Pools.  Each app pool runs under its own w3wp.exe worker process. 
    • In IIS, it's easy to see an application.  A new website is a separate application and any subfolder can be marked as an application.  When they are, the icon beside the folder turnes into a picture of some gears.  By right-clicking on the folder, you have the option of marking a folder as an application or removing it as an application, if it already is one.  Also, in IIS6, in the Application Pools section, you can see all of the applications and which app pool they live under.
    • ASP.NET, on the other hand, doesn't give much visibility into AppDomains, at least not from any visual tools.  This is done behind the scenes.  Programmatically you can create them, tear them down or see a list of all running AppDomains.
    • You can recycle an application from IIS.  In IIS5, you can't do it directly unless you recycle the entire web server, but in IIS6 and greater, you can recycle the application pool that the application lives under.  It will gracefully die off and a new application will start up to replace it.  Or, to word it another way, another w3wp.exe worker process will be started and then the old one will die off after it completes any currently running page requests.
    • You can recycle an AppDomain in ASP.NET through the 'touch trick'.  There are a few ways to do it, but the most straight forward is to edit your web.config file in notepad and add a space to an insignificant place.  Then save the file.  This will cause the AppDomain to recycle.  This *does not* touch the IIS application though.
    • Recycling an AppDomain will come pretty close to starting ASP.NET fresh again for that particular ASP.NET application, so although it doesn't recycle the apppool, it can give ASP.NET a fresh start in many situations.

posted on 2016-01-28 19:11  Dev_Eric  阅读(501)  评论(0编辑  收藏  举报