Simple Injector Documentation Release 5
Total Page:16
File Type:pdf, Size:1020Kb
Simple Injector Documentation Release 5 Simple Injector Contributors Jul 27, 2021 Contents 1 Quick Start 3 1.1 Overview.................................................3 1.2 Getting started..............................................3 1.3 A Quick Example............................................4 1.3.1 Dependency Injection......................................4 1.3.2 Introducing Simple Injector...................................5 1.4 More information.............................................6 2 Using Simple Injector 7 2.1 Resolving instances...........................................9 2.2 Configuring Simple Injector....................................... 10 2.2.1 Auto-Registration/Batch-registration.............................. 13 2.3 Collections................................................ 14 2.3.1 Collection types......................................... 16 2.3.2 Auto-registering collections.................................. 16 2.3.3 Adding registrations to an existing collection......................... 17 2.4 Verifying the container’s configuration................................. 17 2.5 Automatic constructor injection / auto-wiring.............................. 18 2.6 More information............................................. 19 3 Object Lifetime Management 21 3.1 Transient Lifestyle............................................ 22 3.2 Singleton Lifestyle............................................ 22 3.3 Scoped Lifestyle............................................. 23 3.3.1 Disposing a Scope....................................... 24 3.3.2 Asynchronous disposal..................................... 25 3.3.3 Order of disposal........................................ 26 3.4 Thread Scoped Lifestyle......................................... 27 3.5 Async Scoped Lifestyle (async/await).................................. 28 3.6 Web Request Lifestyle.......................................... 29 3.7 Async Scoped lifestyle vs. Web Request lifestyle............................ 30 3.8 WCF Operation Lifestyle........................................ 30 3.9 Per Graph Lifestyle............................................ 31 3.10 Instance Per Dependency Lifestyle................................... 31 3.11 Per Thread Lifestyle........................................... 31 3.12 Per HTTP Session Lifestyle....................................... 31 3.13 Hybrid Lifestyle............................................. 31 i 3.14 Developing a Custom Lifestyle..................................... 32 3.15 Generics and Lifetime Management................................... 34 3.16 Collections and Lifetime Management................................. 34 3.17 Flowing scopes.............................................. 37 4 Integration Guide 41 4.1 Console Application Integration Guide................................. 41 4.2 ASP.NET Core and ASP.NET Core MVC Integration Guide...................... 43 4.2.1 Available integration packages................................. 45 4.2.2 Wiring custom middleware................................... 47 4.2.3 Cross wiring ASP.NET and third-party services........................ 48 4.2.4 Working with IOptions<T> .................................. 48 4.2.5 Using Hosted Services..................................... 49 4.2.6 Using [FromServices] in ASP.NET Core MVC Controllers.................. 49 4.2.7 Resolving services from MVC’s ValidationContext...................... 50 4.2.8 Using Razor Pages....................................... 50 4.2.9 Overriding the default IServiceScope Reuse Behavior..................... 50 4.2.10 Working with ASP.NET Core Identity............................. 51 4.3 .NET Generic Host Integration Guide.................................. 52 4.3.1 Using Hosted Services..................................... 53 4.4 ServiceCollection Integration Guide................................... 55 4.4.1 Cross wiring framework and third-party services....................... 56 4.4.2 Disposing the Container.................................... 59 4.4.3 Integrating with Microsoft Logging.............................. 59 4.4.4 Integrating with Microsoft Localization............................ 60 4.4.5 Working with IOptions<T> .................................. 61 4.5 Blazor Server App Integration...................................... 61 4.6 Azure Functions Integration....................................... 68 4.7 ASP.NET MVC Integration Guide.................................... 73 4.8 ASP.NET Web API Integration Guide.................................. 74 4.8.1 Basic setup........................................... 74 4.8.2 Extra features.......................................... 75 4.9 ASP.NET Web Forms Integration Guide................................. 79 4.10 OWIN Integration Guide......................................... 81 4.10.1 Basic setup........................................... 81 4.10.2 Extra features.......................................... 82 4.11 Windows Forms Integration Guide.................................... 83 4.12 WCF Integration Guide......................................... 84 4.12.1 WAS Hosting and Non-HTTP Activation........................... 85 4.13 Windows Presentation Foundation Integration Guide.......................... 87 4.13.1 Step 1:.............................................. 87 4.13.2 Step 2:.............................................. 87 4.13.3 Step 3:.............................................. 88 4.13.4 Usage.............................................. 89 4.14 Other Technologies............................................ 90 4.15 Patterns.................................................. 90 5 Diagnostic Services 93 5.1 Supported Diagnostic Warnings..................................... 93 5.1.1 Diagnostic Warning - Lifestyle Mismatches.......................... 93 5.1.2 Diagnostic Warning - Short-Circuited Dependencies..................... 98 5.1.3 Diagnostic Warning - Torn Lifestyle.............................. 100 5.1.4 Diagnostic Warning - Ambiguous Lifestyles.......................... 101 5.1.5 Diagnostic Warning - Disposable Transient Components................... 103 ii 5.2 Supported Information Messages.................................... 105 5.2.1 Diagnostic Warning - Potential Single Responsibility Violations............... 105 5.2.2 Diagnostic Warning - Container-registered Types....................... 108 5.3 How to view diagnostic results...................................... 109 5.4 Suppressing warnings.......................................... 112 5.5 Limitations................................................ 113 6 How To 115 6.1 Register factory delegates........................................ 115 6.1.1 Lazy............................................... 118 6.2 Resolve instances by key......................................... 119 6.3 Register multiple interfaces with the same implementation....................... 122 6.4 Override existing registrations...................................... 122 6.5 Verify the container’s configuration................................... 123 6.6 Work with dependency injection in multi-threaded applications.................... 124 6.7 Package registrations........................................... 127 7 Advanced Scenarios 129 7.1 Generics................................................. 129 7.2 Batch-Registration / Auto-Registration................................. 130 7.3 Registration of open-generic types.................................... 132 7.4 Mixing collections of open-generic and non-generic components.................... 134 7.5 Unregistered type resolution....................................... 135 7.6 Context-based injection......................................... 136 7.6.1 Making contextual registrations based on the parent’s metadata................ 137 7.6.2 Making contextual registrations based on the parent’s parent................. 138 7.7 Property injection............................................ 138 7.7.1 Implicit property injection................................... 138 7.7.2 Explicit property injection................................... 138 7.7.3 Enabling property injection................................... 139 7.7.4 IPropertySelectionBehavior................................... 139 7.7.5 Property Injection limitations.................................. 140 7.8 Covariance and Contravariance..................................... 141 7.9 Registering plugins dynamically..................................... 142 7.10 Accessing a dependency’s metadata................................... 143 8 Aspect-Oriented Programming 145 8.1 Decoration................................................ 145 8.1.1 Applying Decorators conditionally............................... 147 8.1.2 Applying Decorators conditionally using type constraints................... 147 8.1.3 Decorators with Func<T> decoratee factories......................... 148 8.1.4 Decorated collections...................................... 151 8.1.5 Using contextual information inside decorators........................ 152 8.1.6 Applying decorators conditionally based on consumer..................... 153 8.1.7 Decorator registration factories................................. 154 8.1.8 Applying lifestyles to Decorators................................ 154 8.2 Interception using Dynamic Proxies................................... 156 9 Extensibility Points 159 9.1 Overriding Constructor Resolution Behavior.............................. 159 9.2 Overriding Property Injection Behavior................................. 161 9.3 Overriding Parameter