Skip to content

ruyut/AutoServiceRegistration.AspNetCore

Repository files navigation

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.

For more details, visit the related blog post: AutoServiceRegistration.AspNetCore Example (written in Traditional Chinese).

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).

About

Automatically registers services in ASP.NET Core using marker interfaces for dependency injection.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages