AutoServiceRegistration.AspNetCore 1.0.5

dotnet add package AutoServiceRegistration.AspNetCore --version 1.0.5
                    
NuGet\Install-Package AutoServiceRegistration.AspNetCore -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutoServiceRegistration.AspNetCore" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoServiceRegistration.AspNetCore" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="AutoServiceRegistration.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AutoServiceRegistration.AspNetCore --version 1.0.5
                    
#r "nuget: AutoServiceRegistration.AspNetCore, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AutoServiceRegistration.AspNetCore@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AutoServiceRegistration.AspNetCore&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=AutoServiceRegistration.AspNetCore&version=1.0.5
                    
Install as a Cake Tool

AspNetCore.AutoServiceRegistration

NuGet version (AutoServiceRegistration.AspNetCore) GitHub Actions Workflow Status

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

  1. 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();
    
  2. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 223 2/16/2025
1.0.4 106 2/14/2025
1.0.3 104 2/13/2025
1.0.2 211 2/13/2025
1.0.1 134 2/13/2025 1.0.1 is deprecated.
1.0.0 139 2/13/2025 1.0.0 is deprecated.