理解AppDomain和AppPool
大家都知道,.net写的程序,都是托管的,何为托管?就是让“其他的程序”来管理,也解析运行,什么又在这里充当“其他程序”呢?这里大体上说是CLR(通用语言运行时),这只是大体上的,准确的在底层是怎么去处理托管程序与操作系统间的关系呢?
操作系统上运行的都是进程,这进程是非托管的。现在。我们有一个Demo.exe,他是用.net写的(这里与语言无关了,因为编设成程序集后,都成为IL语言了),当然是一个托管理程序。这里的问题就是怎么把Demo.exe变成一个进程,运行在操作系统的进程中。这里就引出了应用程序域(Application Domain),应用程序域(Application Domain)是“托管理代码与非托管理代码之间的桥梁”(引自《.NET组件编程设计》)
进程,应用程序域,.net程序集(这里是Demo.exe),之间的关系可以见下图:
一个进程中可以有多个应用程序域(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这个对象的时候才进处理。
首先,应用程序池是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进程都是w3wp;
每个IIS应用程序池都有自己的进程AppPool1 用进程3784.AppPool2用进程5044
不同的Asp.net站点用不同的应用程序域;
站点AppTest2和AppTest3在不同的应用程序域,但在相同的进程中。
使用的关键在哪?
应用程序池和应用程序域都可以提供程序隔离,但用途不一样。应用程序池在没有.Net环境下也可以提供程序间的隔离。而应用程序域是.Net提供的隔离方式。
如果你的服务器有上千个站点,你不会采用上千个应用程序池去隔离站点,那是因为,在服务器中开启了太多的进程。尽管如此,有时你需要应用程序池。应用程序池的好处在于你可以配置应用程序池的标识。同样你可以更多灵活的方式去回收应用程序池。而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.
理解AppDomain和AppPool的更多相关文章
- 理解AppDomain
在传统的 Win32的程序中,进程是独立的运行空间, 在一些大型系统中, 通常都是将系统中的核心功能分解出来用独立的进程来处理,一方面是为了能获得更高的系统性能.吞吐量 .另一方面是为了能隔离功能之间 ...
- C#基础--应用程序域(Appdomain)
AppDomain理解 为了保证代码的键壮性CLR希望不同服务功能的代码之间相互隔离,这种隔离可以通过创建多个进程来实现,但操作系统中创建进程是即耗时又耗费资源的一件事,所以在CLR中引入了AppDo ...
- 通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?
什么是.NET?什么是.NET Framework?本文将从上往下,循序渐进的介绍一系列相关.NET的概念,先从类型系统开始讲起,我将通过跨语言操作这个例子来逐渐引入一系列.NET的相关概念,这主要包 ...
- 【转载】通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?
本文转载自:http://www.cnblogs.com/1996V/p/9037603.html [尊重作者原创,转载说明出处!感谢作者“小曾看世界”分享! ] 什么是.NET?什么是.NET Fr ...
- .NET、NET Framewor以及.NET Core的关系(二)
什么是CLR,.NET虚拟机? 实际上,.NET不仅提供了自动内存管理的支持,他还提供了一些列的如类型安全.应用程序域.异常机制等支持,这些 都被统称为CLR公共语言运行库. CLR是.NET类型系统 ...
- NET?.NET Framework?.NET Core?
什么是.NET?什么是.NET Framework?什么是.NET Core? https://www.cnblogs.com/1996V/p/9037603.html 什么是.NET?什么是.NET ...
- 通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core? .Net Web开发技术栈
通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core? 什么是.NET?什么是.NET Framework?本文将从上往下,循序渐进的介绍一系列相关.NET的概念 ...
- 通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?(转)
通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?(转) 一.总结 一句话总结:.NET是一个平台,包含多种语言,比如(C#.Visual Basic.C++/C ...
- 3万字长文概述:通俗易懂告诉你什么是.NET?什么是.NET Framework?什么是.NET Core?
[转载]通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core? 什么是.NET?什么是.NET Framework?本文将从上往下,循序渐进的介绍一系列相关.NET的 ...
随机推荐
- Zabbix配置参数优化
概述:使用zabbix监控服务器已有一段时间,监控的服务器不到100台,发现刷新zabbix页面有卡顿的现象.而且经常报“Zabbix poller processes more than 75% b ...
- Vue Admin 后台管理
https://segmentfault.com/a/1190000009188689
- Win7删除缓存垃圾文件
del /s /f /q C:\*.tmp
- Spring IoC中各个注解的理解和使用
一.把在Spring的xml文件中配置bean改为Spring的注解来配置bean 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的 ...
- 使用命名管道的OVERLAPPED方式实现非阻塞模式编程 .
命令管道是进程间通讯的一种常用方式,对于命令管道的介绍可以参考别的资料和书籍,这里推荐一个<VC++下命名管道编程的原理及实现>这篇博文,写得比较清楚.但是都是介绍了阻塞模式的编程,我这里 ...
- gcc 工作流程
gcc常用参数: 1:-v/--version 2:-I :指定头文件包含路径 3:-c :将汇编文件生成一个二级制文件,得到.o文件 4:-o :指定生产的文件名 5:-g :gdb调试的时候需要加 ...
- 如何用两块硬盘做磁盘阵列的教程Raid 1
如今,市面上的大部分服务器都自带有阵列卡.只要有两块以上硬盘,我们就可以利用服务器自带的阵列卡做磁盘阵列.Raid 1 为例.Raid 1 是磁盘阵列的其中一个系列,将两块硬盘构成磁盘阵列,可以保证数 ...
- Oracle:Authid Current_User的使用
我们知道,用户拥有的role权限在存储过程是不可用的.遇到这种情况,我们一般需要显式授权,如grant create table to usera;但这种方法太麻烦,有时候可能需要进行非常多的授权才能 ...
- python——前端常用的标签
1.meat标签 meta标签的使用 meta标签共有两个属性:http-equiv和name;不同的属性又有不同的参数值,这些不同的参数值就实现了不同的网页功能. name属性 name属性主要用于 ...
- 运行mysql时,提示Table ‘performance_schema.session_variables’ doesn’t exist
第一步:在管理员命令中输入: mysql_upgrade -u root -p --force 第二步:关闭并重启数据库 service mysql stop service mysql start