The Auto-Register package simplifies service registration for ASP.NET Core applications by automatically discovering and registering services based on custom attributes. This package eliminates the need for manually adding services in Program.cs, supports multiple service lifetimes, and ensures no duplicate registrations occur.
With Auto-Register, services are identified using the RegisterAttribute and are automatically registered as self, interface, or base class implementations.
[Nuget] : https://www.nuget.org/packages/Auto-Register
To install Auto-Register, add it to your project using the NuGet Package Manager or .NET CLI:
Install-Package Auto-Register
dotnet add package Auto-Register
Once the Auto-Register package is installed, you can easily use it in your ASP.NET Core project to auto-register services.
Services that need to be registered must be decorated with the RegisterAttribute. This attribute takes the ServiceLifetime (Singleton, Scoped, or Transient) as a parameter.
using AutoRegister;
// Singleton service
[Register(ServiceLifetime.Singleton)]
public class MySingletonService : IMySingletonService
{
// Implementation
}
// Scoped service
[Register(ServiceLifetime.Scoped)]
public class MyScopedService : IMyScopedService
{
// Implementation
}
// Transient service
[Register(ServiceLifetime.Transient)]
public class MyTransientService : IMyTransientService
{
// Implementation
}
In your ASP.NET Core application, use the AddAutoregister extension method to automatically register services from a given assembly.
If using ASP.NET Core 5.0+ with a minimal hosting model (Program.cs), add the auto-registration in the ConfigureServices section
using AutoRegister;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Automatically register services marked with the RegisterAttribute
builder.Services.AddAutoregister(Assembly.GetExecutingAssembly());
var app = builder.Build();
app.Run();
Consider an ASP.NET Core application where you want to automatically register several services:
[Register(ServiceLifetime.Singleton)]
public class AuthService : IAuthService
{
// Singleton service for authentication
}
[Register(ServiceLifetime.Scoped)]
public class ShoppingCartService : IShoppingCartService
{
// Scoped service for managing shopping carts
}
[Register(ServiceLifetime.Transient)]
public class PaymentService : IPaymentService
{
// Transient service for handling payments
}
// Program.cs (ASP.NET Core 5.0+)
var builder = WebApplication.CreateBuilder(args);
// Automatically register services
builder.Services.AddAutoregister(Assembly.GetExecutingAssembly());
var app = builder.Build();
app.Run();
AuthService will be registered as a Singleton
ShoppingCartService will be registered as a Scoped
PaymentService will be registered as a Transient
The services will automatically be resolved and injected where required, without needing to manually specify them in Program.cs
This attribute is used to mark classes for automatic registration. It defines the lifetime of the service (Singleton, Scoped, or Transient) via the constructor.
public RegisterAttribute(ServiceLifetime lifetime)
ServiceLifetime lifetime: Specifies the lifetime of the service to be registered (Singleton, Scoped, or Transient)
[Register(ServiceLifetime.Singleton)]
public class MyService : IMyService
{
// Implementation
}
Services marked with the RegisterAttribute are automatically discovered and registered based on their lifetime (Singleton, Scoped, or Transient)
Classes can be registered not only as themselves but also as their interfaces, abstract or any base class.
Classes that do not implement interfaces or inherit from abstract base classes can still be self-registered in the service collection.
Services are registered only once, preventing multiple registrations of the same type.
Service lifetime is controlled via the RegisterAttribute, making it easy to specify whether a service should be Singleton, Scoped, or Transient
If you want to register services from multiple assemblies, you can pass those assemblies to the AddAutoregister method. For example:
using AutoRegister;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Register services from multiple assemblies
builder.Services.AddAutoregister(AppDomain.CurrentDomain.GetAssemblies());
var app = builder.Build();
app.Run();
Currently, all services marked with the RegisterAttribute in the provided assemblies will be registered. If you want to exclude certain services, you would need to manually intervene before the registration process.
Auto-Register is designed for use any types of .NET applications that support Microsoft Dependency Injection, including:
- ASP.NET Core Applications
- Worker Services
- Blazor Applications
- WPF Applications
- Windows Forms Applications
I am showing some examples through a console application
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var processor = serviceProvider.GetRequiredService<IMyProcess>();
processor.Save(new Customer());
public interface IEntity
{
Guid Id { get; }
}
public interface IRepository<T> where T : IEntity
{
void Save(T entity);
}
[Register(ServiceLifetime.Transient)]
public class Repository<T> : IRepository<T> where T : IEntity, new()
{
public void Save(T entity)
{
Console.WriteLine($"Entity with ID {entity.Id} has been saved.");
}
}
public interface IService<T> where T : IEntity
{
void Process(T entity);
}
[Register(ServiceLifetime.Transient)]
public class Service<T> : IService<T> where T : IEntity, new()
{
private readonly IRepository<T> _repository;
public Service(IRepository<T> repository)
{
_repository = repository;
}
public void Process(T entity)
{
Console.WriteLine($"Processing entity with ID {entity.Id}...");
_repository.Save(entity);
}
}
public class Customer : IEntity
{
public Guid Id { get; private set; }
public Customer()
{
Id = Guid.NewGuid();
}
}
[Register(ServiceLifetime.Transient)]
public class Processor<TRepository, TService, TEntity>
where TRepository : IRepository<TEntity>
where TService : IService<TEntity>
where TEntity : IEntity, new()
{
private readonly TRepository _repository;
private readonly TService _service;
public Processor(TRepository repository, TService service)
{
_repository = repository;
_service = service;
}
public void Execute()
{
TEntity entity = new TEntity();
Console.WriteLine($"Executing for entity with ID {entity.Id}...");
_service.Process(entity);
}
}
[Register(ServiceLifetime.Transient)]
public class AdvanceProcess : Processor<Repository<Customer>, Service<Customer>, Customer>, IMyProcess
{
public AdvanceProcess(Repository<Customer> repository, Service<Customer> service)
: base(repository, service)
{
}
public void Save(Customer entity)
{
Console.WriteLine($"Entity with ID {entity.Id} and the type is {entity.GetType().FullName}");
}
}
public interface IMyProcess
{
void Save(Customer entity);
}
Here are a few more examples that expand on the Advanced Generic Service Processing model with auto-registration and dependency injection in ASP.NET Core.
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<IMyService<int>>();
myService.DoExecute();
public interface IMyService<T>
{
void DoExecute();
}
[Register(ServiceLifetime.Scoped)]
public class MyService<T> : IMyService<T>
{
public void DoExecute()
{
Console.WriteLine($"Executed Scoped Service with type {typeof(T)}");
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<IClosedGenericService<int>>();
myService.Process(99);
public interface IClosedGenericService<T>
{
void Process(T type);
}
[Register(ServiceLifetime.Scoped)]
public class ClosedGenericService : IClosedGenericService<int>
{
public void Process(int type)
{
Console.WriteLine($"Processing Closed Generic Service with int {type}");
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<IMyService<int, string>>();
myService.DoExecute(5, "Auto-Register");
public interface IMyService<T1, T2>
{
void DoExecute(T1 type1, T2 type2);
}
[Register(ServiceLifetime.Transient)]
public class MyService : IMyService<int, string>
{
public void DoExecute(int type1, string type2)
{
Console.WriteLine($"Executed Transient with {type1} and {type2}");
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<IMyService>();
myService.DoExecute();
public interface IMyService
{
void DoExecute();
}
[Register(ServiceLifetime.Singleton)]
public class MyService : IMyService
{
public void DoExecute()
{
Console.WriteLine("Non-generic service executed (Singleton)");
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<IMyService>();
myService.DoExecute();
public interface IMyService
{
void DoExecute();
}
[Register(ServiceLifetime.Scoped)]
public class MyService : MyGenericService<int>, IMyService
{
}
[Register(ServiceLifetime.Scoped)]
public class MyGenericService<T>
{
public void DoExecute()
{
Console.WriteLine($"Executed with closed generic type: {typeof(T)}");
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<IMyService<int>>();
myService.DoExecute();
public interface IMyService<T> where T : struct
{
void DoExecute();
}
[Register(ServiceLifetime.Singleton)]
public class MyService<T> : IMyService<T> where T : struct
{
public void DoExecute()
{
Console.WriteLine($"Executed Singleton with {typeof(T)}");
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<IMyService<int>>();
myService.DoExecute();
public interface IMyService<T> where T : struct, IComparable
{
void DoExecute();
}
[Register(ServiceLifetime.Scoped)]
public class MyService<T> : IMyService<T> where T : struct, IComparable
{
public void DoExecute()
{
Console.WriteLine($"Executed Scoped with {typeof(T)} and constraint");
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<IMyService<List<int>>>();
myService.DoExecute();
public interface IMyService<T>
{
void DoExecute();
}
[Register(ServiceLifetime.Transient)]
public class MyService<T> : IMyService<T>
{
public void DoExecute()
{
Console.WriteLine($"Executed Transient with nested {typeof(T)}");
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<HybridService>();
myService.DoExecute<int>();
[Register(ServiceLifetime.Scoped)]
public class HybridService
{
private readonly OpenGenericService<int> _openGenericService;
public HybridService(OpenGenericService<int> openGenericService)
{
_openGenericService = openGenericService;
}
public void DoExecute<T>()
{
Console.WriteLine("Executing Scoped HybridService.");
_openGenericService.DoExecute();
}
}
[Register(ServiceLifetime.Scoped)]
public class OpenGenericService<T>
{
public void DoExecute()
{
Console.WriteLine($"Executing Scoped OpenGenericService with type: {typeof(T)}.");
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<LayeredService>();
myService.DoExecute<int>();
public abstract class BaseService
{
public abstract void DoExecute<T>();
}
[Register(ServiceLifetime.Singleton)]
public class GenericBaseService<U> : BaseService
{
public override void DoExecute<T>()
{
Console.WriteLine("Executing Singleton GenericBaseService with type: " + typeof(T));
}
}
[Register(ServiceLifetime.Singleton)]
public class LayeredService : GenericBaseService<int>
{
public override void DoExecute<T>()
{
Console.WriteLine("Executing Singleton LayeredService.");
base.DoExecute<T>();
}
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetRequiredService<IServiceFactory>()
.GetService("A");
myService.DoExecute<string>();
public interface IService
{
void DoExecute<T>();
}
[Register(ServiceLifetime.Scoped)]
public class ServiceImplementationA : IService
{
public void DoExecute<T>()
{
Console.WriteLine($"Executing Scoped ServiceImplementationA with type: {typeof(T)}.");
}
}
[Register(ServiceLifetime.Scoped)]
public class ServiceImplementationB : IService
{
public void DoExecute<T>()
{
Console.WriteLine($"Executing Scoped ServiceImplementationB with type: {typeof(T)}.");
}
}
[Register(ServiceLifetime.Scoped)]
public class ServiceFactory : IServiceFactory
{
private readonly IServiceProvider _serviceProvider;
public ServiceFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public IService GetService(string implementationType)
{
return implementationType switch
{
"A" => _serviceProvider.GetRequiredService<ServiceImplementationA>(),
"B" => _serviceProvider.GetRequiredService<ServiceImplementationB>(),
_ => throw new ArgumentException($"Service type {implementationType} is not supported.")
};
}
}
public interface IServiceFactory
{
IService GetService(string implementationType);
}
using AutoRegister;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddAutoregister(Assembly.GetExecutingAssembly());
using var serviceProvider = services.BuildServiceProvider();
var myCar = serviceProvider.GetRequiredService<ElectricCar>();
myCar.Drive();
[Register(ServiceLifetime.Scoped)]
public class Vehicle
{
public virtual void StartEngine()
{
Console.WriteLine("Engine started.");
}
}
public abstract class LandVehicle : Vehicle
{
public abstract void Drive();
}
public interface ITransport
{
void GetTransportMode();
}
[Register(ServiceLifetime.Scoped)]
public class Car<T> : LandVehicle, ITransport where T : Vehicle, new()
{
public override void Drive()
{
Console.WriteLine("Driving on the road.");
}
public void GetTransportMode()
{
Console.WriteLine("Land transport");
}
public override void StartEngine()
{
Console.WriteLine("Car engine started.");
}
}
[Register(ServiceLifetime.Scoped)]
public class ElectricCar : Car<ElectricCar>
{
public override void Drive()
{
Console.WriteLine("Driving silently on electric power.");
}
}
The Auto-Register NuGet package provides a powerful and flexible way to manage service registration in ASP.NET Core. By automating service discovery and registration, it reduces boilerplate code and helps maintain clean and maintainable service registration logic, especially in large projects with many services.