1 / 38

CLR 、 Assembly and .NET Deployment Features

CLR 、 Assembly and .NET Deployment Features. -- 電算中心 90 年度第二學期讀書會 報告人 try 政治大學電算中心. 參考資料. [1]Thuan Thai,Hoang Q.Lam, “ .NET Framework Essentials ” ,O ’ reilly,June 2001 [2]Serge Lidin, ” Inside Micorosoft .NET IL Assembler ” ,Microsoft Press,2002

Télécharger la présentation

CLR 、 Assembly and .NET Deployment Features

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CLR、Assembly and .NET Deployment Features --電算中心90年度第二學期讀書會 報告人 try 政治大學電算中心

  2. 參考資料 • [1]Thuan Thai,Hoang Q.Lam,“.NET Framework Essentials” ,O’reilly,June 2001 • [2]Serge Lidin,”Inside Micorosoft .NET IL Assembler”,Microsoft Press,2002 • [3]”Microsoft .NET Framework FAQ”,Microsoft Corporation,July 2001 • [4]Gray Cornell,Jonathan Morrison,”Programming VB.NET: A Guide for Experienced Programmers”,Apress 2002 • [5]Jesse Liberty,”Programming C#”,O’reilly, 2001 • [6]Ben Albahari,Peter Drayton,Brad Merrill,”C#Essentials”,O’reilly,2001

  3. Presentation Agenda • 本章學習目的及內容簡介 • .NET與.NET Framework • CLR • 解析.NET可執行檔 • .NET Deployment 特色 • Assembly 觀念 • VEE結構探討 • .NET與COM/Native Dll的互通性

  4. .NET與.NET Framework • .NET • .NET is Microsoft strategy for delivering software as a service. • .NET Framework • An environment for building,deploying and running Web Services and applications. • 三個組成部份:CLR、Framework classes及ASP.NET

  5. CLR的定位 • .NET應用程式的執行時期環境(Run-time) • 提供.NET應用程式和底層作業系統的中間層

  6. CLR和JVM • 很多書都拿CLR來和JVM類比,其實就直觀來看,兩者有很多差別。 • Java程式就實體上看來都是一個一個的.class檔。 • .NET程式都是標準PE格式的exe檔(這裏面存的不是Native Code!!)。 • 我們要執行某一個Java程式時,要打java (class名稱) • .NET程式和一般執行方式一樣,直接執行該exe檔即可。

  7. CLR和JVM(2) • Java的deploy方式是以class檔為最小單位。 • .NET中deploy是以assembly為最小單位。 • Java的class檔中含bytecode,並沒有metadata。(所以classpath要在命令列中註明) • .NET的可執行檔中含MSIL及metadata。 • Microsoft的作品為了體貼使用者,隱藏了許多細節,對Developer來說,反而較不直觀。也是觀念不清的來源。

  8. CLR的功能 • Activate Objects • 對Objects執行安全性檢查(verifier) • layout objects in memory(ClassLoader) • JIT Compilation(JIT Compiler) • Excution support and management • GC • Debug • security • exception handling

  9. Windows上的可執行檔(PE/COFF) • Windows上的可執行檔都必須遵循PE/COFF格式。(Portable Excutable/Microsoft Common Object File Format)。 • 標準的PE可執行檔分成二大部份 • headers • native image sections(.data,.rdata,.rsrc,.text)

  10. Win32 PE

  11. .NET的可執行檔(.NET PE) • 為了可擴充性,Microsoft允許除了這些區段之外,在PE中自定新的section。 • 安裝.NET時,會將OS的loader換掉,換成認得.NET PE的loader。 • .NET正是利用擴充section的方式,.NET的windows loader認得.NET PE,並將這些sections交由CLR處理。 • 這就是為何.NET應用程式也可以以.exe方式存在並直接執行的原因。

  12. .NET PE

  13. 觀察.NET的可執行檔 • 利用dumpbin.exe可以dump出PE檔案。 • 範例 • win32:win32all.txt • .net:helloall.txt • 我們可以發現.NET的section都是在section#1。

  14. .NET Deployment 特色 • Side by side excution • Any versions of the same shared assembly to execute at the same time,on the same system,even in the same process。(在同一個Process或系統中,不同版本的同名Dll檔案可以同時被存在並被執行)。

  15. 範例:Assembly generation utility(al.exe) • al.exe /flags:0x0000完全Side By Side • al.exe /flags:0x0010,同ap內不可多版本並存 • al.exe /flags:0x0020,同Process內不可多版本並存 • al.exe /flags:0x0030,同電腦內不可多版本並存

  16. .NET Deployment 特色(2) • Xcopy deployment • Assembly具有自我描述功能,所以免註冊。 • 為何具有自我描述功能?以assembly中的metadata來取代registry功能。 • 什麼是assembly?什麼是metadata?

  17. Managed Compiler • 指將.NET語言compile成「abstract intermediate form」的工具。如VB.NET的vbc.exe, VC#.NET的csc.exe等。 • abstract intermediate form包括二部份 • Metadata(描述程式之結構及相關資訊) • Managed code(MSIL)

  18. Managed Code • Managed指由CLR來管理及執行的程式碼。 • Managed Code有三個部份由CLR來管理 • Type control:檢查型別、轉換型別。 • Exception Handling • GC(記憶體回收)

  19. Assembly 觀念 • 什麼是Assembly • 什麼是module • Assembly執行過程 • Manifest • Assembly Identities • Shared Assembly

  20. Assembly及Module • Assembly • .NET中Deploy及Versioning的最小單位。 • 指一個功能完整的.NET應用程式。(managed) • Module • 泛指一個可.NET執行檔(.exe)、.NET library(.dll)或其它資源。(必須為managed code) • 藉由metadata描述和其它modules的關係。 • 一個Assembly可以只包含一個或多個Modules。 • Assembly程式進入點所在稱為Prime Module。

  21. Assembly及Module

  22. Manifest • Manifest就是Assembly的metadata。 • Manifest內含資訊 • assembly相依程式碼的位置 • 組成assembly的各檔案檔名 • Assembly相依程式碼的metadata • assembly的版本資訊 • 型別資訊

  23. Assembly Identities • 在COM時代,使用GUID來做為Component的唯一識別碼。但一長串的數字實在不容易記。 • .NET的private assembly引進namespace的觀念來做唯一識別(類似Java的Package)。但仍不能區分同一支程式的不同版本。 • 因此,若一個assembly要由其它的.NET程式共享時,必須由Strong Name來做識別。 • 什麼是Strong Name

  24. Strong Name • .NET中的唯一識別採用PKC(public key cryptography) • Strong name 的認証過程 • Application creator利用assembly中的「manifest」做出一個hash code,我們稱之「original hash」。 • 利用private key 將「original hash」 加密成「encrypted hash」。 • 「Encrypted hash」及Application creator的「public key 」隨著assembly的manifest發行。

  25. Strong Name(2) • 執行者以Application creator的「public key 」對「Encrypted hash」做解密,得到「original hash」 。 • 執行者以assembly中「manifest」做出一個「original hash」 • 執行者比對這二個「original hash」一不一致。

  26. Shared Assembly • GAC(Global Assembly Cache) • 放置重要的系統共享檔案 • 有可能帶來和傳統registry一樣的問題。 • 建議只在二種情況之下將assembly設為shared assembly • 這個assembly一定要被好幾個Application 所共用。 • 這個assembly需要高安全性。(只有administrator可以動GAC中的檔案)

  27. VEE結構

  28. Class Loader • OS loader認出.NET PE後,將控制權交給CLR。此時ClassLoader會起來,找到並載入含進入點Main()的Class。 • 其它的Classes在第一次被參考到時也會被載入。 • ClassLoader如何找到Classes:目前目錄之.config、GAC及metadata(manifest)。

  29. Method stub • Class Loader將classes放入記憶體中後,會在每個class的method加上一個stub,這個stub有二個作用: • 用來辨別這個method是否已被JIT Compilation。 • 標明這個是managed/unmanaged code

  30. JIT Compiler • .NET PE中所包含的不是native code,而是metadata及IL。 • 直到method第一次被執行前才會由JIT將IL轉成managed native code。(如何得知第一次被執行?利用method stub) • Compile完後將method的native code之位址存回method stub中。

  31. JIT Compiler(2) • 這些編譯後的native code何時被消除 • 直到這個process shutdown ,並由GC程序將process所reference到的memory都回收。 • Pre-JITing • 使用ngen.exe:安裝時期先行編譯。

  32. Verifier • Verifier是JIT Compiler的一部份。 • Class Loader將Class load進來之後,verifier會進行檢查。 • metadata是不是well-formed • IL Code是否Type Safe

  33. Execution Support & Management • GC • Security management • Code management(控制程式碼的執行) • Exception management • debug/profiling(提供debug/profile的API) • marshaling management • thread management

  34. 觀念整理:Assembly執行過程

  35. .NET與COM/Native Dll的互通性 • 呼叫.NET元件 • Regasm.exe:將.NET Assembly註冊到系統的registry中。 • tlbexp:產生該Assembly的type library • 然後VB/VC就可以用呼叫一般COM元件的方式來使用這個.NET元件了。 • 呼叫COM元件 • tlbimp.exe:為com 元件產生一個.NET PE的stub。(會產生一個dll檔) • 對這個.NET stub的呼叫都會被轉成對COM的呼叫。

  36. Summary • .NET和Java執行方式並不相同,CLR及JVM安裝在平台上的方式也不相同。安裝CLR後對整個作業系統影響較大。(ie.把OS的loader給換掉了)。 • .NET的程式利用metadata取代傳統的registry方式來描述自己。從而減少了dll hell的發生機會。 • 所有.NET的程式經過managed compiler後都會變成IL,因此可達到跨語言的功能。但是變成XX.NET的語言都要符合CTS才行。 • CLS的標準只有部份被ECMA接受。

  37. Summary(2) • 所有.NET程式語言都使用相同一組library,所以只要學習一種.NET語言,其它語言都相當容易上手。 • 在windows平台上無論是易用性及效能.NET都有出色的表現。移植到其它OS上表現能否一樣出色則尚待觀察。

  38. Q & A

More Related