Autofac Documentation Release 6.0.0
Total Page:16
File Type:pdf, Size:1020Kb
Autofac Documentation Release 6.0.0 Autofac Project Jul 11, 2021 Contents 1 Getting Started 3 1.1 Structuring the Application.......................................3 1.2 Add Autofac References.........................................5 1.3 Application Startup............................................5 1.4 Application Execution..........................................6 1.5 Going Further..............................................8 1.6 Need Help?................................................8 1.7 Building from Source..........................................8 2 What’s New 9 2.1 Release Notes..............................................9 2.2 Upgrading from Autofac 5.x to 6.x................................... 10 2.3 Upgrading from Autofac 3.x to 4.x................................... 12 3 Registering Components 15 3.1 Registration Concepts.......................................... 15 3.1.1 Reflection Components..................................... 16 3.1.2 Instance Components...................................... 17 3.1.3 Lambda Expression Components................................ 17 3.1.4 Open Generic Components................................... 19 3.1.5 Services vs. Components.................................... 19 3.1.6 Default Registrations...................................... 20 3.1.7 Conditional Registration.................................... 20 3.1.8 Configuration of Registrations................................. 22 3.1.9 Dynamically-Provided Registrations.............................. 22 3.2 Passing Parameters to Register...................................... 22 3.2.1 Available Parameter Types................................... 22 3.2.2 Parameters with Reflection Components............................ 23 3.2.3 Parameters with Lambda Expression Components....................... 23 3.3 Property and Method Injection...................................... 24 3.3.1 Property Injection........................................ 24 3.3.2 Method Injection........................................ 24 3.4 Assembly Scanning........................................... 25 3.4.1 Scanning for Types....................................... 25 3.4.2 Scanning for Modules...................................... 26 3.4.3 IIS Hosted Web Applications.................................. 27 i 4 Resolving Services 29 4.1 Passing Parameters to Resolve...................................... 30 4.1.1 Available Parameter Types................................... 30 4.1.2 Parameters with Reflection Components............................ 30 4.1.3 Parameters with Lambda Expression Components....................... 31 4.1.4 Passing Parameters Without Explicitly Calling Resolve.................... 31 4.2 Implicit Relationship Types....................................... 31 4.2.1 Supported Relationship Types................................. 32 4.2.2 Composing Relationship Types................................. 41 4.2.3 Relationship Types and Container Independence........................ 42 5 Controlling Scope and Lifetime 43 5.1 Basic Concepts and Terminology.................................... 43 5.2 Scopes and Hierarchy.......................................... 44 5.3 Example: Singleton Lifetime...................................... 46 5.4 Example: Web Application....................................... 48 5.5 Additional Topics to Explore....................................... 50 5.5.1 Working with Lifetime Scopes................................. 50 5.5.2 Instance Scope......................................... 52 5.5.3 Captive Dependencies..................................... 58 5.5.4 Disposal............................................. 61 5.5.5 Lifetime Events......................................... 66 5.5.6 Running Code at Container Build............................... 67 6 Configuration 75 6.1 JSON/XML Configuration........................................ 75 6.1.1 Configuring With Microsoft Configuration (4.0+)....................... 76 6.1.2 Configuring With Application Configuration (Legacy Pre-4.0)................ 81 6.2 Modules................................................. 84 6.2.1 Introduction........................................... 84 6.2.2 Advantages of Modules..................................... 84 6.2.3 Example............................................. 85 6.2.4 Adapting to the Deployment Environment........................... 87 6.2.5 Common Use Cases for Modules................................ 87 7 Application Integration 89 7.1 ASP.NET................................................. 89 7.1.1 OWIN.............................................. 89 7.1.2 MVC.............................................. 91 7.1.3 Web API............................................ 99 7.1.4 SignalR............................................. 110 7.1.5 Web Forms........................................... 113 7.2 .NET Core................................................ 119 7.2.1 Quick Start........................................... 120 7.2.2 Using a Child Scope as a Root................................. 121 7.3 ASP.NET Core.............................................. 122 7.3.1 Quick Start........................................... 122 7.3.2 Configuration Method Naming Conventions.......................... 126 7.3.3 Dependency Injection Hooks.................................. 127 7.3.4 Differences From ASP.NET Classic.............................. 127 7.3.5 Controllers as Services..................................... 128 7.3.6 Multitenant Support....................................... 129 7.3.7 Using a Child Scope as a Root................................. 130 7.3.8 Example............................................. 132 ii 7.4 Blazor.................................................. 133 7.5 Windows Communication Foundation (WCF).............................. 133 7.5.1 Clients.............................................. 134 7.5.2 Services............................................. 135 7.6 Service Fabric.............................................. 141 7.6.1 Quick Start........................................... 141 7.6.2 Per-Request Scopes....................................... 142 7.6.3 Example............................................. 142 7.7 Managed Extensibility Framework (MEF)................................ 143 7.7.1 Consuming MEF Extensions in Autofac............................ 143 7.7.2 Providing Autofac Components to MEF Extensions...................... 143 7.7.3 Using Metadata......................................... 144 7.7.4 Known Issues / Gotchas.................................... 144 7.8 Common Service Locator........................................ 144 7.9 Moq.................................................... 145 7.9.1 Getting Started......................................... 145 7.9.2 Configuring Mocks....................................... 146 7.9.3 Configuring Specific Dependencies.............................. 146 7.10 FakeItEasy................................................ 147 7.10.1 Getting Started......................................... 147 7.10.2 Configuring Fakes........................................ 148 7.10.3 Configuring Specific Dependencies.............................. 149 7.10.4 Options for Fakes........................................ 149 8 Best Practices and Recommendations 151 8.1 Always Resolve Dependencies from Nested Lifetimes......................... 151 8.2 Structure Configuration with Modules.................................. 151 8.3 Use As<T>() in Delegate Registrations................................. 151 8.4 Use Constructor Injection........................................ 152 8.5 Use Relationship Types, Not Service Locators............................. 152 8.6 Register Components from Least-to-Most Specific........................... 152 8.7 Use Profilers for Performance Checking................................. 152 8.8 Register Once, Resolve Many...................................... 152 8.9 Register Frequently-Used Components with Lambdas......................... 152 8.10 Consider a Container as Immutable................................... 153 8.11 Optimize or Avoid Diagnostics..................................... 153 9 Advanced Topics 155 9.1 Registration Sources........................................... 155 9.1.1 “Any Concrete Type Not Already Registered” Source..................... 155 9.1.2 Implementing a Registration Source.............................. 155 9.2 Adapters and Decorators......................................... 158 9.2.1 Adapters............................................. 158 9.2.2 Decorators............................................ 159 9.3 Composites................................................ 162 9.3.1 Metadata............................................ 164 9.3.2 Lifetime............................................. 164 9.3.3 Decorators............................................ 164 9.3.4 Composites and Collections.................................. 165 9.4 Circular Dependencies.......................................... 165 9.4.1 Property/Property Dependencies................................ 165 9.4.2 Constructor/Property Dependencies.............................. 166 9.4.3 Constructor/Constructor Dependencies............................. 166 9.5 Component Metadata / Attribute Metadata............................... 167 iii 9.5.1 Adding Metadata to a Component Registration........................ 167 9.5.2 Consuming Metadata...................................... 167 9.5.3 Strongly-Typed Metadata.................................... 168 9.5.4 Interface-Based Metadata...................................