You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46-2Lines changed: 46 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,7 @@ Source generator that helps register attribute marked services in the dependency
15
15
- Transient, Singleton, Scoped service registration
16
16
- Factory registration
17
17
- Module method registration
18
+
- Host-aware module registration with `IHostApplicationBuilder`
18
19
- Duplicate Strategy - Skip,Replace,Append
19
20
- Registration Strategy - Self, Implemented Interfaces, Self With Interfaces
20
21
- Decorator registration (`RegisterDecorator`) — no runtime dependencies
@@ -332,7 +333,7 @@ public class FrontEndLoggingDecorator : IService
332
333
333
334
#### Register Method
334
335
335
-
When the service registration is complex, use the `RegisterServices` attribute on a method that has a parameter of`IServiceCollection`or `ServiceCollection`
336
+
When the service registration is complex, use the `RegisterServices` attribute on a method whose first parameter is`IServiceCollection`, `ServiceCollection`, or `IHostApplicationBuilder`. The method may optionally receive a supported string tag collection as its second parameter.
336
337
337
338
```c#
338
339
publicclassRegistrationModule
@@ -351,15 +352,58 @@ public class RegistrationModule
351
352
}
352
353
```
353
354
355
+
Register methods can inspect the tags passed to the generated extension method and conditionally add services. Supported tag parameters include `IEnumerable<string>`, `IReadOnlySet<string>`, `IReadOnlyCollection<string>`, `ICollection<string>`, `ISet<string>`, and `HashSet<string>`.
The source generator creates an extension method with all the discovered services registered. Call the generated extension method to add the services to the container. The extension method will be called `Add[AssemblyName]`. The assembly name will have the dots removed.
389
+
The source generator creates an `IServiceCollection`extension method named `Add[AssemblyName]`, with dots removed from the assembly name. This extension registers discovered services and decorators, then invokes registration methods whose first parameter is `IServiceCollection` or `ServiceCollection`.
357
390
358
391
```c#
359
392
varservices=newServiceCollection();
360
393
services.AddInjectioTestsConsole();
361
394
```
362
395
396
+
When the consuming project references `Microsoft.Extensions.Hosting.Abstractions`, the generator also creates an overload for `IHostApplicationBuilder`. Injectio does not add this dependency to DI-only projects.
397
+
398
+
The host extension first calls the service collection extension through `builder.Services`, then invokes registration methods whose first parameter is `IHostApplicationBuilder`. Calling the host extension is therefore sufficient to run both service and host registrations.
399
+
400
+
```c#
401
+
varbuilder=Host.CreateApplicationBuilder(args);
402
+
builder.AddInjectioTestsConsole();
403
+
```
404
+
405
+
Generated registration methods are idempotent. Service collection registrations and host registrations are tracked independently, so calling the service extension before the host extension still runs each portion once. For each portion, the tags supplied to its first invocation determine the registrations; later invocations are ignored.
406
+
363
407
Override the extension method name by using the `InjectioName` MSBuild property.
0 commit comments