.NET and .NET Core 2. .NET Runtimes Pan Wuming 2017
Copyright © 2012 by Jeffrey Richter
.NET runtimes Common Language Runtime (CLR) for the .NET Framework Core Common Language Runtime (CoreCLR) for .NET Core .NET Native for Universal Windows Platform The Mono runtime for Xamarin.iOS, Xamarin.Android, Xamarin.Mac, and the Mono desktop framework
Topics .NET Application Common Language Runtime (CLR) Assembly: Application Unit Metadata Runtime host Application domains
IL: intermediate language .NET Application A .NET Application contains one or more Assemblies An assembly contains Metadata and IL Code, and resources, within one or many files A .NET Application must run in a host environment The execution of a .NET Application is Managed by CLR IL: intermediate language Self-Host?
Managed execution process Choosing a compiler. Compiling your code to MSIL. Compiling MSIL to native code. Running code. Managed By CLR Create Assembly
Common Language Runtime (CLR)
CLR Services Class Layout Security JIT Compiler Other Runtime Services Type Awareness Exception Integration JIT Compiler Other Runtime Services Class Layout Memory & GC Security
The Roles of CLR Compiler Assembly Development Source code C# VB F# … public static void Main(String[] args ) usr=Environment.GetEnvironmentVariable("USERNAME"); try { { String usr; FileStream f; StreamWriter w; w.WriteLine(usr); w=new StreamWriter(f); f=new FileStream(“C:\\test.txt",FileMode.Create); Console.WriteLine("Exception:"+e.ToString()); } catch (Exception e){ w.Close(); } Compiler public static void Main(String[] args ) usr=Environment.GetEnvironmentVariable("USERNAME"); try { { String usr; FileStream f; StreamWriter w; w.WriteLine(usr); w=new StreamWriter(f); f=new FileStream(“C:\\test.txt",FileMode.Create); Console.WriteLine("Exception:"+e.ToString()); } catch (Exception e){ w.Close(); } Source code C# VB F# … MSIL Metadata Resources
Global Assembly Cache (GAC) Application Directory The Roles of CLR Assembly Development Compiler Assembly Development C# VB F# … MSIL Metadata Resources public static void Main(String[] args ) usr=Environment.GetEnvironmentVariable("USERNAME"); try { { String usr; FileStream f; StreamWriter w; w.WriteLine(usr); w=new StreamWriter(f); f=new FileStream(“C:\\test.txt",FileMode.Create); Console.WriteLine("Exception:"+e.ToString()); } catch (Exception e){ w.Close(); } Source code Global Assembly Cache (GAC) Install Setup Copy Browser Application Directory Download Cache
The Roles of CLR Assembly on Target Machine Development Application Directory Setup Copy Browser Download Cache Deployment Assembly on Target Machine Global Assembly Cache (GAC) Install Policy <security> <mscorlib> <configuration> <?xml version="1.0" encoding="utf-8" ?> <policy> version="1" <CodeGroup class="UnionCodeGroup" <PolicyLevel version="1"> PermissionSetName="Nothing" Description="Code group grants no permissio ns and forms the root of the code group tree."> Name="All_Code" <IMembershipCondition clas s="AllMembershipCondition" version="1"/> PermissionSetName="FullTrust" Execution IL to Native Compiler Class Loader Security Assembly Loader Garbage Collection Native code + GC table Code Manager Exception Manager Thread Support COM Interop Debug Engine
Benefits of Presence of the CLR Performance improvements Cross-language integration Cross-language exception handling Enhanced security Versioning and deployment support A simplified model for component interaction Debugging and profiling services.
Assembly: Application Unit Contains Code Security boundary. Type boundary Reference scope boundary Version boundary Deployment unit To support Side-by-side execution
Manifest Enumerates the files that make up the assembly. Governs how references to the assembly's types and resources map to the files that contain their declarations and implementations. Enumerates other assemblies on which the assembly depends. Provides a level of indirection between consumers of the assembly and the assembly's implementation details. Renders the assembly self-describing.
GAC and Strong-Named Assembly Global Assembly Cache: Machine-wide Code Cache Strongly Named Assemblies Are Tamper-Resistant Strong-Named the simple text name the version number optional culture information a digital signature, and the token of the public key that corresponds to the private key used for signing. Delay Signing or Partial Signing an Assembly Assemblies deployed in the GAC must have a strong name
Signing an assembly
The AssemblyRef metadata information
Metadata within a PE File PE section Contents of PE section PE header The index of the PE file's main sections the address of the entry point. MSIL instructions The Microsoft intermediate language instructions (MSIL) Accompanied by metadata tokens. Metadata Metadata tables and heaps Every type and member Custom attributes Security information. Win32 Portable Executable File Format
Metadata Tables and Heaps Each metadata table holds information about a certain type of elements of your program Method, type, and so on Metadata also stores information in four heap structures String Blob: binary objects of arbitrary size user string GUID: 16-byte binary objects Metadata tokens are used in MSIL to reference a row of a particular metadata table.
Application Domains Application domains are typically created by runtime hosts To provide isolation between assemblies Objects that pass between domains are either copied or accessed by proxy
Runtime Hosts Shell executables IE ASP.NET Before any managed code can be executed, the host must load and initialize the CLR. Loads the runtime into a process Creates the application domains within the process Loads user code into the application domains The hosting APIs All hosts start with an unmanaged stub because the runtime is not yet running in the process. C++! #include <metahost.h> #pragma comment(lib, "mscoree.lib") … hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
Finally, These are the key words: Common Language Runtime (CLR) Assembly Metadata Manifest Global Assembly Cache(GAC) Strong Named Assembly Intermediate language (IL) Delay Sign JIT Complier Garbage collection (GC)