应用程序池与应用程序域的区别(Different between Application Pool and AppDomain )

题外:

最近遇到了静态变量生命周期无法正常回收的问题。一个静态变量Timer 在修改了Web.Config之后会变成2个。前面一个不能正常的释放。最后发现前面一个Timer所在的AppDomain也没有释放。如何释放前一个AppDomain,或许看完此文会有答案。


I saw a lot of people asking what"s the differences between Application Pool and AppDomain in ASP.NET.
我看到有很多朋友问关于Asp.Net 应用程序池与应用程序域的问题。
 

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 + "</br>";
        info += "Current Process Id:" + Process.GetCurrentProcess().Id + "</br>";

        info += "Current Application Domain:"+AppDomain.CurrentDomain.FriendlyName + "</br>";
        info += "Current Application Domain Base Dir:"+AppDomain.CurrentDomain.BaseDirectory + "</br>";
        
        
        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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值