You are previewing Customizing the Microsoft® .NET Framework Common Language Runtime.
O'Reilly logo
Customizing the Microsoft® .NET Framework Common Language Runtime

Book Description

Get advanced programming techniques and guidelines to exploit the flexibility and customizability of the common language runtime for a variety of applications, from databases to cell phones.

Table of Contents

  1. Customizing the Microsoft® .NET Framework Common Language Runtime
  2. Foreword
  3. Acknowledgments
  4. Introduction
    1. What Makes This .NET Framework Book Unique?
    2. The .NET Framework 2.0 Prereleases
    3. Sample Code and System Requirements
    4. Comments, Corrections, and Support
  5. 1. CLR Hosts and Other Extensible Applications
    1. Summary
  6. 2. A Tour of the CLR Hosting API
    1. CorBindToRuntimeEx and ICLRRuntimeHost
    2. CLR Hosting Managers
    3. CLR Initialization and Startup
    4. Other Unmanaged Functions on mscoree.dll
    5. Hosting Manager Discovery
      1. Discovering Host-Implemented Managers
        1. Step 1: The Host Supplies a Host Control Class
        2. Step 2: The CLR Queries the Host Control Class
      2. Obtaining CLR-Implemented Managers
    6. Overview of the Hosting Managers
      1. Assembly Loading
      2. Customizing Failure Behavior
      3. Programming Model Enforcement
      4. Memory and Garbage Collection
      5. Threading and Synchronization
      6. Other Hosting API Features
        1. Loading Code Domain Neutral
        2. Thread Pool Management
        3. I/O Completion Management
        4. Debugging Services Management
        5. Application Domain Management
        6. CLR Event Handling
    7. Summary
  7. 3. Controlling CLR Startup and Shutdown
    1. The CLR Startup Configuration Settings
      1. Version
        1. Side by Side: A Technique to Avoid DLL Hell
        2. The Side-by-Side Architecture of the .NET Framework
          1. .NET Framework Registry Keys
          2. The Versioned Installation Directory
          3. The Global Assembly Cache
        3. The CLR Startup Shim
        4. .NET Framework Updates
        5. Choosing Your Strategy: Fix or Float
      2. The Server and Workstation Builds
      3. Concurrent Garbage Collection
      4. Domain-Neutral Code
    2. Setting the Startup Options Using CorBindToRuntimeEx
    3. Starting the CLR
    4. Handling Failures from CorBindToRuntimeEx
    5. Deferring CLR Initialization and Startup
    6. The CLR and Process Lifetime
    7. Summary
  8. 4. Using the Default CLR Host
    1. Invoking the Default Host: Running Managed Executables
    2. Invoking the Default Host: Activating Managed Types Through COM Interop
    3. Defaults for the CLR Startup Options
      1. Selecting a CLR Version
        1. Running Managed Executables
          1. Upgrades
        2. Activating Managed Types Through COM Interop
    4. Customizing the Default Host Using Configuration Files
      1. Concurrent Garbage Collection
      2. Build Type
        1. Changing the Build Type on Older Versions of the CLR
      3. Version
        1. The <supportedRuntime> Element
        2. The <requiredRuntime> Element and .NET Framework 1.0
    5. Summary
  9. 5. Using Application Domains Effectively
    1. The Role of Application Domains
      1. Type Safety and Verification
      2. Application Isolation
        1. Type Visibility
        2. Configuration Data
        3. Security Settings
        4. Access to Static Data and Members
        5. Runtime Concepts Not Isolated by Application Domains
    2. Application Domain Structure
      1. Assembly List
      2. Security Policy
      3. Application Domain Properties
      4. Statics for Domain-Neutral Assemblies
      5. Proxies for Remote Calls
      6. The Default Application Domain
      7. The AppDomainViewer Tool
    3. Guidelines for Partitioning a Process into Multiple Application Domains
      1. Code Unloading
      2. Cross-Domain Communication
      3. Sample Application Domain Boundaries
        1. ASP.NET
        2. SQL Server
        3. Internet Explorer
    4. Application Domain Managers
      1. Creating an Application Domain Manager
      2. Associating an Application Domain Manager with a Process
        1. The CLR Hosting APIs
          1. Calling an Application Domain Manager from Unmanaged Code
          2. Step 1: Setting Initialization Flags to Register With Host
          3. Step 2: Implement IHostControl::SetAppDomainManager
        2. Environment Variables
      3. Creating Application Domains
    5. Application Domains and Threads
      1. The CLR Thread Pool
    6. Unloading Application Domains
      1. Step 1: Aborting the Threads Running in the Domain
      2. Step 2: Raising an Unload Event
      3. Step 3: Running Finalizers
      4. Step 4: Freeing the Internal CLR Data Structures
      5. Exceptions Related to Unloading Application Domains
      6. Receiving Application Domain Unload Events
        1. Receiving Domain Unload Events Using the IActionOnCLREvent Interface
    7. Summary
  10. 6. Configuring Application Domains
    1. Application Domain Configuration Settings
    2. Private Assembly Directory Settings
      1. Customizing the ApplicationBase Directory Structure
      2. Turning Off ApplicationBase Searching
    3. Configuration File Settings
    4. Shadow Copy Settings
      1. Turning on Shadow Copy
      2. Specifying the Location for the Copied Files
      3. Specifying Which Files Are Copied
      4. The Shadow Copy Sample
    5. Assembly Binding Settings
    6. Miscellaneous Settings
      1. LicenseFile
      2. LoaderOptimization
      3. DynamicBase
      4. DisallowCodeDownload
      5. ActivationArguments
      6. AppDomainInitializer and AppDomainInitializerArguments
    7. Customizing Application Domain Creation Using System.AppDomainManager
      1. Step 1: Call AppDomainManager.CreateDomain
      2. Step 2: Create a New Instance of the Application Domain Manager
      3. Step 3: Call AppDomainManager.InitializeNewDomain
      4. Step 4: Get the ApplicationActivator
      5. Step 5: Get the HostExecutionContextManager
      6. Step 6: Get the HostSecurityManager
    8. Summary
  11. 7. Loading Assemblies in Extensible Applications
    1. Concepts and Terminology
      1. Strong and Weak Assembly Names
      2. Early-Bound and Late-Bound References
      3. Fully Specified and Partially Specified References
      4. Version Policy
    2. Loading Assemblies by Assembly Identity
      1. Architecture of an Extensible Application Revisited
        1. System.Reflection.Assembly and CLR Remote Calls
        2. Recommendations for Loading Assemblies in Extensible Applications
          1. Step 1: The Extensible Application Is Made Aware of the New Add-In
          2. Step 2: An Application Domain Is Chosen in Which to Load the New Add-In
          3. Step 3: The Application Domain Manager in the Target Domain Is Called to Load the Add-In
          4. Step 4: The Application Domain Manager in the Target Domain Loads the Add-In
      2. Using Assembly.Load and Related Methods
        1. Specifying Assembly Identities as Strings
        2. Specifying Assembly Identities Using System.Reflection.AssemblyName
      3. How the CLR Locates Assemblies
        1. How the CLR Locates Assemblies with Weak Names
        2. How the CLR Locates Assemblies with Strong Names
        3. Using System.Reflection.Assembly to Determine an Assembly’s Location on Disk
      4. Using Fuslogvw.exe to Understand How Assemblies Are Located
      5. Common Assembly Loading Exceptions
      6. Partially Specified Assembly References
    3. Loading Assemblies by Filename
      1. Subtleties of Assembly.LoadFrom
        1. LoadFrom’s Second Bind
        2. Binding Contexts and Type Identity
        3. Loading Multiple Files with the Same Name
        4. The Loadfrom Context and Dependencies
      2. The ReflectionOnly APIs
    4. Loading Assemblies Using ICLRRuntimeHost
    5. Capturing Assembly Load Events
    6. Versioning Considerations for Extensible Applications
      1. Determining Which Version of the CLR Was Used to Build an Assembly
      2. The Extensible Application Chooses the Version
      3. Microsoft .NET Framework Unification
      4. Overriding Unification
    7. Summary
  12. 8. Customizing How Assemblies Are Loaded
    1. The Cocoon Deployment Model
      1. CLR Binding Identities
      2. Obtaining Binding Identities
        1. Step 1: Obtaining a Pointer to ICLRAssemblyIdentityManager
        2. Step 2: Calling GetBindingIdentityFromFile (or Stream)
      3. The Makecocoon.exe Program
    2. Implementing an Assembly Loading Manager
      1. Implementing the IHostAssemblyManager Interface
        1. Specifying Non-Host-Loaded Assemblies
        2. Returning an Assembly Store
      2. Implementing the IHostAssemblyStore Interface
        1. Resolving Assembly References
          1. The AssemblyBindInfo Structure
          2. The EBindPolicyLevels Enumeration
          3. Associating Host-Specific Data with an Assembly
          4. Assigning Assembly Identity
          5. Loading Assemblies from a Cocoon
        2. Resolving Module References
      3. Bringing It All Together
        1. Opening the Cocoon File
        2. Initializing the CLR
        3. Creating the Assembly Loading Manager and Host Control Object
        4. Invoking the Hosted Application
    3. Customizing How Assemblies Are Loaded Using Only Managed Code
      1. The Load(byte[]...) Methods
      2. The AssemblyResolve Event
      3. The Runcocoonm Sample
      4. Supporting Multifile Assemblies
    4. Summary
  13. 9. Domain-Neutral Assemblies
    1. Domain-Neutral Assembly Architecture
      1. Implications of Using Domain-Neutral Code
        1. Domain-Neutral Code Cannot Be Unloaded
        2. Access to Static Member Variables Is Slower
        3. Initialization of Types Can Be Slower
        4. Security Policy Must Be Consistent Across All Application Domains
        5. The Set of Domain-Neutral Assemblies Must Form a Closure
      2. Domain-Neutral Code and Assembly Dependencies
    2. Specifying Which Assemblies Are Loaded Domain Neutral
      1. CorBindToRuntimeEx
      2. The Loader Optimization API
      3. Domain-Neutral Assemblies and the CLR Hosting Interfaces
    3. Determining Whether an Assembly Has Been Loaded Domain Neutral
    4. Summary
  14. 10. Extending the CLR Security System to Protect Your Extensible Application
    1. An Overview of Code Access Security
      1. The Core Concepts: Evidence, Permissions, and Policy
        1. Evidence
        2. Policy
          1. Policy Levels
          2. Default CAS Policy
        3. Permissions
      2. Runtime Enforcement of Permissions: Permission Demands and the Stack Walk
    2. Customizing the Code Access Security System Using a HostSecurityManager
    3. Code Access Security in the Cocoon Host
      1. Step 1: Provide an Initial Implementation of HostSecurityManager
      2. Step 2: Create Custom Evidence
      3. Step 3: Create a Custom Membership Condition
      4. Step 4: Create an Application-Domain-Level Policy Tree
      5. Step 5: Assign Custom Evidence to Assemblies in the Cocoon
        1. Assigning Evidence Using the Assembly Loading APIs
      6. Putting It All Together
    4. Associating Evidence with an Application Domain
    5. The AllowPartiallyTrustedCallers Attribute
    6. Summary
  15. 11. Writing Highly Available Microsoft .NET Framework Applications
    1. Application Domain Isolation and Process Lifetimes
      1. Failure Escalation
      2. Critical Finalization, SafeHandles, and Constrained Execution Regions
    2. Specifying Escalation Policy Using the CLR Hosting Interfaces
      1. Setting Policy Using ICLRPolicyManager
        1. Step 1: Obtain a ICLRPolicyManager Interface Pointer
        2. Step 2: Set Actions to Take on Failures
        3. Step 3: Set Timeouts and the Actions to Take for Various Operations
        4. Step 4: Set Any Default Actions
        5. Step 5: Specify Unhandled Exceptions Behavior
      2. Receiving Notifications Through IHostPolicyManager
        1. Step 1: Provide an Implementation of IHostPolicyManager
        2. Step 2: Notify the CLR of Your Implementation Using IHostControl
    3. Guidelines for Writing Highly Available Managed Code
      1. Use SafeHandles to Encapsulate All Native Handles
      2. Use Only the Synchronization Primitives Provided by the .NET Framework
      3. Ensure That Calls to Unmanaged Code Return to the CLR
      4. Annotate Your Libraries with the HostProtectionAttribute
    4. Summary
  16. 12. Enforcing Application-Specific Programming Model Constraints
    1. The Host Protection Categories
      1. Synchronization
      2. Shared State
      3. External Process Management
      4. Self-Affecting Process Management
      5. Self-Affecting Threading
      6. External Threading
      7. Security Infrastructure
      8. User Interface
      9. "May Leak on Abort"
    2. Using the Host Protection Manager
    3. Host Protection in the Cocoon Deployment Model
      1. Step 1: Obtaining the ICLRHostProtectionManager Interface Pointer
      2. Step 2: Specifying Which Host Protection Categories to Block
    4. Summary
  17. 13. Managing How the CLR Uses Memory
    1. Integrating the CLR with Custom Memory Managers
      1. Virtual Memory Management
      2. Heap Management
      3. File Mapping
      4. Reporting Memory Status to the CLR
        1. The GetMemoryLoad Method
        2. The ICLRMemoryNotificationCallback Interface
    2. Configuring the CLR Garbage Collector
      1. Partitioning the Garbage Collector’s Heap
      2. Gathering Garbage Collection Statistics
      3. Initiating Garbage Collections
      4. Receiving Notifications Through the IHostGCManager Interface
    3. Summary
  18. 14. Integrating the CLR with Custom Schedulers and Thread Pools
    1. The Task Abstraction
    2. Controlling the Execution of Tasks
      1. The Life Cycle of a Task
      2. Hooking Calls That Enter and Leave the CLR
      3. The Synchronization Manager
      4. Replacing the CLR’s Thread Pool
    3. Summary
  19. About the Author
  20. Index
  21. About the Author
  22. Copyright