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 Startup.cs, supports multiple service lifetimes, and ensures no duplicate registrations occur.
With AutoRegister, services are identified using the RegisterAttribute and are automatically registered as self, interface, or base class implementations.
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 6.0+ with a minimal hosting model (Program.cs), add the auto-registration in the ConfigureServices section
using AutoRegister;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Automatically register services marked with the RegisterAttribute
builder.Services.AddAutoregister(Assembly.GetExecutingAssembly());
var app = builder.Build();
app.Run();
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 and abstract base classes.
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(Assembly.GetExecutingAssembly());
builder.Services.AddAutoregister(typeof(SomeExternalService).Assembly);
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.
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 6+)
var builder = WebApplication.CreateBuilder(args);
// Automatically register services
builder.Services.AddAutoregister(Assembly.GetExecutingAssembly());
var app = builder.Build();
app.Run();
#### The services will automatically be resolved and injected where required, without needing to manually specify them in Program.cs
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.