### Registering an `IHostedService` Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Illustrates how to use the `[RegisterHostedService]` attribute to automatically register a class implementing `IHostedService`, simplifying background service setup. ```C# [RegisterHostedService] class Foo; ``` ```C# serviceCollection.AddHostedService(); ``` -------------------------------- ### Install AutoRegisterInject NuGet Package Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Provides the XML `PackageReference` entry for adding AutoRegisterInject to a .NET project via NuGet, enabling its source generation capabilities. ```XML ``` -------------------------------- ### Configuring ServiceCollection from Multiple Assemblies Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Shows how to use assembly-specific `AutoRegisterFromAssemblyName` methods to consolidate dependency registrations from various projects into a single `ServiceCollection`, simplifying multi-project setups. ```C# var serviceCollection = new ServiceCollection(); serviceCollection.AutoRegisterFromMyProjectMain(); serviceCollection.AutoRegisterFromMyProjectServices(); serviceCollection.AutoRegisterFromMyProjectData(); serviceCollection.BuildServiceProvider(); ``` -------------------------------- ### Basic Scoped Registration and Generated Code Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Illustrates how marking a class with `[RegisterScoped]` triggers the automatic generation of a `ServiceCollection` extension method for scoped registration, providing a compile-time alternative to manual setup. ```C# namespace MyProject; [RegisterScoped] public class Foo { } ``` ```C# internal IServiceCollection AutoRegister(this IServiceCollection serviceCollection) { serviceCollection.AddScoped(); return serviceCollection; } ``` -------------------------------- ### Applying Registrations with `AutoRegister()` Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Demonstrates how to initialize `ServiceCollection` and invoke the `AutoRegister()` extension method to apply all generated dependency injection registrations within your application startup. ```C# var serviceCollection = new ServiceCollection(); serviceCollection.AutoRegister(); serviceCollection.BuildServiceProvider(); ``` -------------------------------- ### Class Registration with `[RegisterScoped]` and Direct Output Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Shows the minimal class declaration with the `[RegisterScoped]` attribute and the direct `AddScoped` call it generates, demonstrating the simplicity of registration. ```C# [RegisterScoped] class Foo; ``` ```C# serviceCollection.AddScoped(); ``` -------------------------------- ### Registering a Class via Multiple Implemented Interfaces Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Demonstrates how AutoRegisterInject registers a class for each of its multiple implemented interfaces, allowing the class to be resolved through any of them. ```C# [RegisterTransient] class Bar : IBar, IFoo, IBaz; ``` ```C# serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); ``` -------------------------------- ### Registering a Class with Multiple Explicit Interfaces in C# Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Illustrates how to register a class with multiple specific interfaces by providing multiple type arguments to the `RegisterScoped` attribute. This allows for fine-grained control over which interfaces a class is registered against. ```csharp [RegisterScoped(typeof(IBar), typeof(IFoo))] public class Foo; ``` -------------------------------- ### Registering a Class with a Single Explicit Interface in C# Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Demonstrates how to explicitly register a class with only one of its implemented interfaces using the `RegisterScoped` attribute. This causes other interfaces implemented by the class to be ignored by AutoRegisterInject. The second code block shows the resulting service collection registration. ```csharp public interface IFoo { } public interface IBar { } [RegisterScoped(typeof(IBar))] public class Foo; ``` ```csharp serviceCollection.AddTransient(); ``` -------------------------------- ### Registering a Class via a Single Implemented Interface Source: https://github.com/patrickklaeren/autoregisterinject/blob/main/README.md Explains how attributes like `[RegisterTransient]` register a class with its single implemented interface (e.g., `IBar`), requiring resolution through the interface rather than the concrete type. ```C# [RegisterTransient] class Bar : IBar; ``` ```C# serviceCollection.AddTransient(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.