理解AppDomain和AppPool
大家都知道,.net写的程序,都是托管的,何为托管?就是让“其他的程序”来管理,也解析运行,什么又在这里充当“其他程序”呢?这里大体上说是CLR(通用语言运行时),这只是大体上的,准确的在底层是怎么去处理托管程序与操作系统间的关系呢?
操作系统上运行的都是进程,这进程是非托管的。现在。我们有一个Demo.exe,他是用.net写的(这里与语言无关了,因为编设成程序集后,都成为IL语言了),当然是一个托管理程序。这里的问题就是怎么把Demo.exe变成一个进程,运行在操作系统的进程中。这里就引出了应用程序域(Application Domain),应用程序域(Application Domain)是“托管理代码与非托管理代码之间的桥梁”(引自《.NET组件编程设计》)
进程,应用程序域,.net程序集(这里是Demo.exe),之间的关系可以见下图:
应用程序域(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这个对象的时候才进处理。
首先,应用程序池是IIS中的概念,而应用程序域是.net中的概念。你可以写一段程序运行在2个或更多的应用程序域上。
我在我的Vista电脑IIS7 Aps.net2.0上做了个测试。
下面是测试代码:
{
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 Base Dir:"+AppDomain.CurrentDomain.BaseDirectory + "
";
divInfo.InnerHtml = info;
}
}
现在,我建立2个应用程序池AppPool1和AppPool2;
下面是返回结果:
Current Process Id:3784
Current Application Domain:/LM/W3SVC/1/ROOT/AppTest1-2-128701111683637820
Current Application Domain Base Dir:C:\inetpub\wwwroot\AppTest\
Current Process Id:5044
Current Application Domain:/LM/W3SVC/1/ROOT/AppTest2-1-128701111868733395
Current Application Domain Base Dir:C:\inetpub\wwwroot\AppTest\
Current Process Id:5044
Current Application Domain:/LM/W3SVC/1/ROOT/AppTest3-2-128701113026462030
Current Application Domain Base Dir:C:\inetpub\wwwroot\AppTest\
如果你的服务器有上千个站点,你不会采用上千个应用程序池去隔离站点,那是因为,在服务器中开启了太多的进程。尽管如此,有时你需要应用程序池。应用程序池的好处在于你可以配置应用程序池的标识。同样你可以更多灵活的方式去回收应用程序池。而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.