標籤:
應用程式集區:這是微軟的一個全新概念:應用程式集區是將一個或多個應用程式連結到一個或多個背景工作處理序集合的配置。因為應用程式集區中的應用程式與其他應用程式被背景工作處理序邊界分隔,所以某個應用程式集區中的應用程式不會受到其他應用程式集區中應用程式所產生的問題的影響。
應用程式定義域:
大家都知道,.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.aspxCurrent 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.aspxCurrent 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.aspxCurrent 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.
理解AppDomain和AppPool