C# AppDomain_第1頁
C# AppDomain_第2頁
C# AppDomain_第3頁
C# AppDomain_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、.NET   Framework   類庫     AppDomain   類     表示應(yīng)用程序域,它是一個(gè)應(yīng)用程序在其中執(zhí)行的獨(dú)立環(huán)境。無法繼承此類。   命名空間:System 程序集:mscorlib(在   mscorlib.dll   中) 應(yīng)用程序域(由   AppDomain   對(duì)象表示)為執(zhí)行托管代碼提供隔離、卸載和安全邊界。 使用應(yīng)用程序域隔離可能終止進(jìn)程的任務(wù)。如果正在執(zhí)行任務(wù)的  

2、; AppDomain   的狀態(tài)變得不穩(wěn)定,則可以卸載   AppDomain,但不會(huì)影響進(jìn)程。當(dāng)進(jìn)程必須不重新啟動(dòng)而長時(shí)間運(yùn)行時(shí),這一點(diǎn)很重要。還可使用應(yīng)用程序域隔離不應(yīng)共享數(shù)據(jù)的任務(wù)。 如果程序集被加載到默認(rèn)應(yīng)用程序域中,則當(dāng)進(jìn)程運(yùn)行時(shí)將無法從內(nèi)存中卸載該程序集。但是,如果打開另一個(gè)應(yīng)用程序域來加載和執(zhí)行程序集,則卸載該應(yīng)用程序域時(shí)也會(huì)同時(shí)卸載程序集。使用此技術(shù)最小化長時(shí)間運(yùn)行的進(jìn)程的工作集,這些進(jìn)程偶爾會(huì)使用大型   DLL。   多個(gè)應(yīng)用程序域可以在一個(gè)進(jìn)程中運(yùn)行;但是,在應(yīng)用程序域和線程之間沒有一對(duì)一的關(guān)聯(lián)。多個(gè)線程可以屬

3、于一個(gè)應(yīng)用程序域,盡管給定的線程并不局限于一個(gè)應(yīng)用程序域,但在任何給定時(shí)間,線程都在一個(gè)應(yīng)用程序域中執(zhí)行。 應(yīng)用程序域通過使用   CreateDomain   方法來創(chuàng)建。AppDomain   實(shí)例用于加載和執(zhí)行程序集   (Assembly)。當(dāng)不再使用   AppDomain   時(shí),可以將它卸載。 AppDomain   類實(shí)現(xiàn)一組事件,這些事件使應(yīng)用程序可以在加載程序集、要卸載應(yīng)用程序域或引發(fā)未處理的異常時(shí)進(jìn)行響應(yīng)。 有關(guān)使用應(yīng)用程序域的更多信息,請(qǐng)參見應(yīng)用程序域。 此類實(shí)

4、現(xiàn)   MarshalByRefObject、_AppDomain   和   IEvidenceFactory   接口。 在任何情況下都不應(yīng)創(chuàng)建   AppDomain   對(duì)象的可遠(yuǎn)程控制的包裝。這樣做可發(fā)布對(duì)該   AppDomain   的遠(yuǎn)程引用,將諸如   CreateInstance   方法向遠(yuǎn)程訪問公開,并有效損壞該   AppDomain   的代碼訪問安全性。連接到遠(yuǎn)程   AppDomain   的惡意客戶端可以獲得對(duì)

5、  AppDomain   本身可訪問的所有資源的訪問權(quán)。您不應(yīng)為任何以下類型創(chuàng)建可遠(yuǎn)程控制的包裝:擴(kuò)展   MarshalByRefObject   的類型和實(shí)現(xiàn)惡意客戶端可用來繞過安全系統(tǒng)的方法的類型。 此示例顯示如何創(chuàng)建新的   AppDomain,在該新建   AppDomain   中實(shí)例化類型,以及與該類型的對(duì)象通信。此外,此示例還顯示如何卸載導(dǎo)致對(duì)象被垃圾回收的   AppDomain。 using   System; using   System.R

6、eflection; using   System.Threading; class   Module1          public   static   void   Main()                          /   Get   and   display  

7、the   friendly   name   of   the   default   AppDomain.                 string   callingDomainName   =   Thread.GetDomain().FriendlyName;                 Console.Wri

8、teLine(callingDomainName);                 /   Get   and   display   the   full   name   of   the   EXE   assembly.                 string   exeAssembly &#

9、160; =   Assembly.GetEntryAssembly().FullName;                 Console.WriteLine(exeAssembly);                 /   Construct   and   initialize   settings   for   a   sec

10、ond   AppDomain.                 AppDomainSetup   ads   =   new   AppDomainSetup();                 ads.ApplicationBase   =               &#

11、160;           "file:/ "   +   System.Environment.CurrentDirectory;                 ads.DisallowBindingRedirects   =   false;                 ads.DisallowC

12、odeDownload   =   true;                 ads.ConfigurationFile   =                           AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;    

13、             /   Create   the   second   AppDomain.                 AppDomain   ad2   =   AppDomain.CreateDomain( "AD   #2 ",   null,   ads);     

14、0;           /   Create   an   instance   of   MarshalbyRefType   in   the   second   AppDomain.                  /   A   proxy   to   the   object   is &#

15、160; returned.                 MarshalByRefType   mbrt   =                           (MarshalByRefType)   ad2.CreateInstanceAndUnwrap(         &#

16、160;                       exeAssembly,                                   typeof(MarshalByRefType).FullName            

17、            );                 /   Call   a   method   on   the   object   via   the   proxy,   passing   the                

18、   /   default   AppDomain 's   friendly   name   in   as   a   parameter.                 mbrt.SomeMethod(callingDomainName);                 /   Unload &#

19、160; the   second   AppDomain.   This   deletes   its   object   and                   /   invalidates   the   proxy   object.                 AppDoma

20、in.Unload(ad2);                 try                                          /   Call   the   method   again.   Note 

21、60; that   this   time   it   fails                           /   because   the   second   AppDomain   was   unloaded.                

22、        mbrt.SomeMethod(callingDomainName);                         Console.WriteLine( "Sucessful   call. ");                        &#

23、160;         catch(AppDomainUnloadedException)                                          Console.WriteLine( "Failed   call;   this   is   expected

24、. ");                            /   Because   this   class   is   derived   from   MarshalByRefObject,   a   proxy   /   to   a   MarshalByRefTy

25、pe   object   can   be   returned   across   an   AppDomain   /   boundary. public   class   MarshalByRefType   :   MarshalByRefObject          /     Call   this   method 

26、0; via   a   proxy.         public   void   SomeMethod(string   callingDomainName)                          /   Get   this   AppDomain 's   settings  

27、and   display   some   of   them.                 AppDomainSetup   ads   =   AppDomain.CurrentDomain.SetupInformation;                 Console.WriteLine( "AppName=0, 

28、0; AppBase=1,   ConfigFile=2 ",                           ads.ApplicationName,                           ads.ApplicationBase,      

29、;                     ads.ConfigurationFile                 );                 /   Display   the   name   of   the   calling  

30、; AppDomain   and   the   name                   /   of   the   second   domain.                 /   NOTE:   The   application 's   thread   has   transitioned   between                   /   AppDomains.  

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論