1 / 52

Overview of the .NET CLR and Friends

Overview of the .NET CLR and Friends. Sam Gentile ManagedCode Consulting. .NET Consultant, Speaker INETA Speaker, Win-Dev Speaker Working in managed code for over 3 years with Microsoft , Groove, 3Leaf Solutions, Pacific Mindworks, NaviSite (3 .NET products shipped + more on way)

thy
Download Presentation

Overview of the .NET CLR and Friends

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. Overview of the .NET CLR and Friends Sam Gentile ManagedCode Consulting

  2. .NET Consultant, Speaker INETA Speaker, Win-Dev Speaker Working in managed code for over 3 years with Microsoft, Groove, 3Leaf Solutions, Pacific Mindworks, NaviSite (3 .NET products shipped + more on way) Designed and implemented Groove Toolkit for Microsoft Visual Studio .NET using VSIP, C#, COM Interop, Managed C++, etc. (JOLT Winner) .NET Writer and Community MSDN Columnist “Beyond Add Reference” MSDN C# Community Site WinForms for Managed C++ Programmers, Chris Sells & Sam Gentile, MSDN April 2003 .NET DJ April 2003 “Developing GUI Applications with MC++” Co-author Wrox Visual C++.NET – Interop, COM Interop O’Reilly articles and upcoming book Blog and Contacts Major .NET Blogger at http://samgentile.com/blog/ .NET home at http://www.samgentile.com/ ManagedCode@comcast.com Who is this Guy?

  3. In the beginning, there was DOS, statically linked C/C++ libraries ….and the world was bad Then came the DLL for reuse … shared functions resolved at runtime Still no binary encapsulation, no versioning, etc. Then came COM …and the world was LOVE Contracts as type definitions instead of function entry points Set the Controls for the Heart of the Sun

  4. Clients program to interfaces, not classes Code not statically linked; demand-loaded at runtime Express world in interfaces that obey binary layout model Language neutral (in theory!) DCOM is COM with a longer wire! All You Need Is Love

  5. Brocksmidian COM inherently Unscalable! MTS and COM+ distinguish state & behavior Infrastructure for state management Ask the runtime to provide services – Interceptors Common pooling model Common transaction coordination Houston, We Have a Problem!

  6. COM has been very successful but… Two ways of describing contract IDL and type libraries – neither complete Binary contract -> precise v-table offsets or everything breaks! No attempt to describe component dependencies Lack of extensibility and scalability - MTS VB programmers could not directly influence TLB Biggest problem: required above average attention spans from developer Attempts to hide ala VB binary compatibility farce have only compounded problem COM is Dead

  7. 1997 PDC COM+ 3.0, …No wait…, COR, Lightening, NGWS… Common Language Infrastructure (CLI) is the spec Common Language Runtime (CLR) is the (Microsoft) implementation The CLI is: ECMA Standardized specification for a virtual execution environment Common Type System (CTS) Metadata (Partition II) Common Language Specification (CTS) Virtual Execution System (VES) Common Intermediate Language (CIL) – Partition III Standard Libraries (Base Class Library – BCL) Enter the CLI/CLR

  8. Unified type system (CTS) Package types into completely self-describing, portable units Execute code under control of a runtime that can enforce runtime policy Delay layout & compilation until runtime Load types so isolated at runtime Types can be verified as type-safe CLI Concepts

  9. Common Type System (CTS) defines all types for all languages Metadata to describe and reference CTS types Metadata also allows CLI to: Locate/load classes Lay out instances in memory Resolve method invocations Translate CIL to native code Setup runtime context boundaries Its all about Metadata, Baby

  10. CLR is an abstraction, a VM Common Intermediate Language (CIL) Microsoft Intermediate Language (MSIL) Processor independent instruction set All .NET compilers -> IL + Metadata CLR never executes CIL directly! CIL always translated into machine code CIL/MSIL

  11. Common Language Runtime(CLR)

  12. Common Language Runtime (CLR) and BCL/FCL Framework SDK is free download FCL is very feature rich – BCL + Data + WinForms + ASP.NET, etc. Simplified programming model True versioning – forget DLL Hell! Simplified deployment – forget Registry! The .NET Framework

  13. .NET Framework

  14. Programming language integration Simplified code reuse Type-safe verification Rich debugging support Always exceptions Security Interop RAD Development!! .NET Framework Benefits

  15. APL Multilingual Development Fortran Perl Scheme Smalltalk Python Ada Component Pascal Oberon Mercury Visual Basic Delphi C# Oz C++ J# Mondrian C Pascal JScript Haskell Java Eiffel Cobol RPG Objective Caml ML

  16. Languages are immaterial in the CLR Languages exist merely to express semantics of CLR, BCL Level playing field Learn the CLR and BCL first! Then pick a syntax to express it It’s The Runtime Stupid!

  17. Logicalunit of deployment, reuse, versioning and security Can be independently versioned Physically one or more managed modules and/or resources as PE file Single file with definitions for types IL + metadata, external types One of modules must have manifest Assembly can be single or multi-file Has location-independent name Assemblies

  18. C#Source CodeFile(s) BasicSource Code File(s) JScriptSource CodeFile(s) C++Source CodeFile(s) C#Compiler BasicCompiler JScriptCompiler C++Compiler Managed Module(IL and Metadata) Compiling Source Codeinto Managed Modules

  19. Combining Managed Modulesinto Assemblies Managed Module(IL and Metadata) Tool combiningmultipleManaged ModulesandResource Filesinto an AssemblyC# compiler (CSC.exe),VB Compiler(VBC.exe),Assembly Link (AL.exe),… Assembly(Manifest)The manifestdescribes a collection ofManaged Modulesand Resource files Managed Module(IL and Metadata) Resource file(.jpeg, .gif, .html, etc.) Resource file(.jpeg, .gif, .html, etc.)

  20. Demo! .NET Languages Creating Assemblies

  21. Assembly Parts

  22. IL Disassembler Your .NET Best Friend! Displays IL & metadata for .NET PE Assembly Ildasm /? For options Ildasm /adv to see metadata, COR Header ILDASM

  23. Even better than ILDASM! Should be installed right after .NET http://www.aisto.com/roeder/dotnet/ Super class browser w/nice hierarchical namespace view Dissassembler, decompiler, XML doc browser (C# and VB) Assembly & namespace views Reflector

  24. Ilasm – IL assembler Al – assembly linker Fuslogvw.exe - Assembly Binding Log Viewer Gacutil – Global assembly cache tool Dumpbin /clrheader PEVerify – standalone verifier Cordbg – CLR Debugger Other Useful Tools

  25. Demo! Looking Inside Assemblies with ILDASM, Reflector

  26. All assemblies have four-part name for an unique ID Friendly Name (file name sans extension) Version in 4 parts (Major.Minor.Build.Rev) CultureInfo specifies language and region Public key identifies the developer Display Name = Simple Name + comma delimited list of version, culture, key Fully qualified = all four parts specified Guess what partially specified means.. Private (Simple) versus Shared Assemblies Assembly Names

  27. Assembly’s Manifest contains version number list of all referenced assemblies with their version numbers Form <major>.<minor>.<build>.<revision> Unless strong-named doesn’t have versioning from CLR perspective Two assemblies differing only by version considered completely different assemblies by CLR AssemblyVersion attribute in metadata Default - run app only with the versions they were built and tested with Assembly Version Basics

  28. Private assemblies installed in app’s directory or subdirectories (recommended) Probing uses app’s AppBase For EXE, directory where app started For Web app, in general, root of virtual dir Probing looks AppBase itself Subdirectory with same name as assembly Can configure to probe other subdirectories via privatePath element in app config file Version numbers ignored for private assemblies Simple Assembly Resolution

  29. Probing via Application Config Files <?xml version="1.0"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="Shared;Common" /> </assemblyBinding> </runtime> </configuration>

  30. Supports assembly versioning Shared assemblies must be strongly named Public/private key pair to form unique ID Also prevents tampering Has filename, version, culture + signed with publisher’s private key Strong Name utility SN.EXE to create key pair Apply AssemblyKeyFile or AssemblyDelaySign attributes to associate key with assembly Can now deploy to GAC or directory outside APPBASE (via CODEBASE) Strongly-Named Assemblies

  31. CLR can run load & execute multiple versions of app or component simultaneously Multiple versions of CLR can run side-by-side Application runs with single version of framework built and tested with This is the most important part of answer to versioning problem Different philosophy than COM and Win32 Components in .NET do NOT have to maintain backward-compatibility (but should) Control given to admins for versioning policies Side-by-side Assemblies and Execution

  32. Machine-wide cache for shared assemblies – Global Assembly Cache Shared assemblies that have strong names Only when necessary Shared location Side-by-side versioning File security Additional search location Native images of assemblies ngen’d Copies of assemblies downloaded remotely The Assembly Cache

  33. Assembly Resolver maps names to physical file on disk with requested version If not strongly named, looks in AppBase Algorithm Apply versioning policy Check if version already loaded Look in the GAC Look in the codebase specified in configuration file Probe Lots of customization possible (diagram ahead) can make things confusing Can also explicitly load with URL via Assembly.LoadFrom Assembly Binding

  34. Assembly resolution and loading

  35. Redirect to load a different version Only applied to assemblies with strong names Can be specified at three levels in order: Application (app config file) Publisher policy Machine policy (via machine.config) Binding controlled by assemblyBinding element Controlled via bindingRedirect and publisherPolicy sub-elements Applying Versioning Policies

  36. Example Versioning Policy <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name=“Foo.dll” publicKeyToken=“154192caa47878ff “ culture=“neutral”/> <bindingRedirect oldVersion=“1.0.0.0” newVersion=“2.0.0.0”/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>

  37. What happens if major bug found? As “publisher” of a .NET component, need way to change policy when fix bug, new version Publisher policies are XML config files Format of file same as others Create publisher policy by running AL.EXE Deployed with new version Must be installed in the GAC Applying Publisher Policies

  38. Example Publisher Policy <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name=“Foo.dll” publicKeyToken=“154192caa47878ff “ culture=“neutral”/> <bindingRedirect oldVersion=“1.3.0.0” newVersion=“2.0.0.0”/> <codebase version=“2.0.0.0” href=“http://www.foo.com/Foo.dll” /> ….

  39. Al /out:policy.2.0.foo.dll /version:1.2.0.0 /keyfile:myKeys.snk /linkresource:foo.config Gacutil –I policy.2.0.0.foo.dll Using AL to Create Policy

  40. Demo! .NET Language Integration Creating Strongly-Named Assemblies The GAC

  41. Assembly Language Compiler Native Code JIT Compiler Execution Compilation And Execution Compilation Code (IL) Source Code Metadata

  42. Managed ExecutionCalling a method for the first time Console static void WriteLine() static void WriteLine(string) (remaining members) Managed EXE Static void Main() { Console.WriteLine(“Hello”) Console.WriteLine(“Vermont”) } JITCompiler JITCompiler Native code … MSCorEE.dll JITCompiler function { • In the assembly that implements the type (Console), look up the method (WriteLine) being called in the metadata. • From the metadata, get the IL for this method. • Allocate a block of memory. • Compile the IL into native code; save the code in the memory allocated in step 3. • Modify the method’s entry in the Type’s table so that it now points to the memory block allocated in step 3. • Jump to the native code contained inside the memory block. }

  43. The CTS

  44. Allocated on managed heap Must be allocated with new() Garbage collected Can be null Passed by reference Reference Types

  45. Performance: allocated on stack Not controlled by GC Derived from System.ValueType Passed by value Primitive types mapped to value types Structs, enums are value types Value Types

  46. Everything in .NET an Object Boxing converts value type to heap based reference Int i = 42; Object box = i; Unboxing converts heap reference to value type equivalent Int j = (int) box; Boxing/Un-boxing

  47. P/Invoke Not that useful Static entry points in DLLs COM Interop Call COM from .NET, .NET from COM MC++ “It Just Works” (IJW) More efficient thunk Interop

  48. Interop MotivationsTwo Hugely Different Worlds! • Unmanaged Code • Type libraries (incomplete!) • Reference counted • Immutable types • DLL issues/Hell • Interface based • HRESULTS • GUIDS Managed Code • Meta data • Garbage collected • Resilient bind • Assemblies • Object based • Exceptions • Strong names

  49. Motivations, Goals and BridgesCOM Interop Pictorially

  50. Learn the CLR, BCL first! Follow Microsoft naming conventions Pascal case for Methods (i.e. HelloWorld) Camel for names (i.e. someVariable) No Hungarian! Consider using FCL types (i.e. Int32) Use Enterprise Services for transactions Best Practices

More Related