AutoServiceRegistration.AspNetCore
1.0.5
dotnet add package AutoServiceRegistration.AspNetCore --version 1.0.5
NuGet\Install-Package AutoServiceRegistration.AspNetCore -Version 1.0.5
<PackageReference Include="AutoServiceRegistration.AspNetCore" Version="1.0.5" />
<PackageVersion Include="AutoServiceRegistration.AspNetCore" Version="1.0.5" />
<PackageReference Include="AutoServiceRegistration.AspNetCore" />
paket add AutoServiceRegistration.AspNetCore --version 1.0.5
#r "nuget: AutoServiceRegistration.AspNetCore, 1.0.5"
#:package AutoServiceRegistration.AspNetCore@1.0.5
#addin nuget:?package=AutoServiceRegistration.AspNetCore&version=1.0.5
#tool nuget:?package=AutoServiceRegistration.AspNetCore&version=1.0.5
AspNetCore.AutoServiceRegistration
Overview
AspNetCore.AutoServiceRegistration is a library for automatically registering services in ASP.NET Core applications. It simplifies dependency injection setup by scanning all loaded assemblies for classes that implement specific marker interfaces, and then registering them with the corresponding service lifetime.
Features
- Automatic Service Registration: Scans assemblies to automatically register services that implement designated interfaces.
- Lifetime Management: Supports Singleton, Scoped, and Transient service lifetimes through marker interfaces:
ISingletonService
for Singleton services.IScopedService
for Scoped services.ITransientService
for Transient services.
- Ease of Use: Simply add the extension method in your service configuration to enable automatic registration.
Installation
Install the package via .NET CLI:
dotnet add package AutoServiceRegistration.AspNetCore
Alternatively, add a project reference to your ASP.NET Core application.
Usage
Register Services
Add the auto service registration extension in your service configuration:
// add using directive using AutoServiceRegistration.AspNetCore; var builder = WebApplication.CreateBuilder(args); // register service builder.Services.AddRegisterServices(); var app = builder.Build();
Implement the Service Class
Create a class that implements your service interface. Since it implements a marker interface, it will be automatically registered:
public interface IMyService : IScopedService { }
public class MyService : IMyService { }
[ApiController] [Route("users")] public class MyController : ControllerBase { private readonly IMyService _myService; public MyController(IMyService myService) { _myService = myService; } }
In this implementation, the service class MyService implements the marker interface IScopedService, which signals the auto registration mechanism to register it with a scoped lifetime. This means that whenever a dependency on IMyService is requested (for instance, in the MyController constructor), the dependency injection container will provide an instance of MyService with the appropriate lifecycle management automatically.
How It Works
The AddRegisterServices
extension method:
- Uses reflection to scan all assemblies loaded in the current AppDomain.
- Filters out non-abstract classes that implement the specified marker interfaces.
- Registers each discovered service with the dependency injection container using the corresponding lifetime (Singleton, Scoped, or Transient).
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
-
net8.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.