ASP.NET Core Scrutor extension for automatic registration of classes inherited from IScopedLifetime, ISelfScopedLifetime, ITransientLifetime, ISelfTransientLifetime, ISingletonLifetime, ISelfSingletonLifetime
EasyScrutor is a modernized fork of the original Scrutor.AspNetCore project. The main goals of this fork were to:
- Modernize the library with updated dependencies and best practices
- Remove the misleading "AspNetCore" from the name - while this library works great with ASP.NET Core, it also works with any .NET application using dependency injection (console apps, worker services, etc.)
- Continue development with a clear path forward, as discussions with the original maintainer indicated the original project might not receive active updates
The original project was created by sefacan and provided a simple, convention-based approach to dependency injection. This fork maintains that simplicity while ensuring compatibility with modern .NET versions.
Major improvements in Scrutor dependency (v4.2.0 -> v7.0.0):
- Keyed service registration support - Leverage .NET 8's keyed DI for more advanced scenarios
- Interface filtering for
AsSelfWithInterfaces- Better control over which interfaces to register - Security fix - Resolved System.Text.Json vulnerability through DependencyModel update
- Performance improvements - Optimized decoration performance for applications with many services
- Generic type support - Better handling of generic ServiceDescriptor from C# 11+
- .NET 6 and .NET 8 support - Full compatibility with modern .NET versions
- Bug fixes:
- Fixed multiple decoration layers for generic types
- Made DecoratedType and IsDecorated method public for extensibility
- Improved error handling when scanning assemblies
- Fixed generic type creation exceptions
| Build server | Platform | Status |
|---|---|---|
| Github Actions | All | |
| Code Coverage | All | |
| NuGet | Package | |
| NuGet | Downloads | |
| GitHub | Release | |
| License | MIT |
Get started with EasyScrutor in just 3 steps:
1. Install the package:
dotnet add package EasyScrutor2. Mark your services with a lifetime interface:
public interface IMyService { string GetMessage(); }
public class MyService : IMyService, IScopedLifetime
{
public string GetMessage() => "Hello from EasyScrutor!";
}3. Register in Program.cs:
builder.Services.AddEasyScrutor();That's it! Your services are now automatically registered and ready to inject anywhere in your application.
Install the EasyScrutor NuGet Package.
Install-Package EasyScrutor
dotnet add package EasyScrutor
EasyScrutor automatically discovers and registers your services by scanning for classes that implement lifetime marker interfaces.
Implement one of the lifetime marker interfaces on your service classes:
IScopedLifetime- Registers as ScopedITransientLifetime- Registers as TransientISingletonLifetime- Registers as SingletonISelfScopedLifetime- Registers as Scoped (self-registration, no interface)ISelfTransientLifetime- Registers as Transient (self-registration, no interface)ISelfSingletonLifetime- Registers as Singleton (self-registration, no interface)
using EasyScrutor;
public interface IDataService
{
Task<string> GetDataAsync();
}
// This class will be automatically registered as Scoped
public class DataService : IDataService, IScopedLifetime
{
public async Task<string> GetDataAsync()
{
return await Task.FromResult("Hello from DataService!");
}
}
⚠️ BREAKING CHANGE: The method signature has been updated. Please useAddEasyScrutor()instead ofAddAdvancedDependencyInjection()for clearer, more intuitive naming.
ASP.NET Core:
var builder = WebApplication.CreateBuilder(args);
// Add EasyScrutor - automatically scans and registers services
builder.Services.AddEasyScrutor();
var app = builder.Build();
app.Run();Console/Worker Service:
var builder = Host.CreateApplicationBuilder(args);
// Add EasyScrutor - automatically scans and registers services
builder.Services.AddEasyScrutor();
var host = builder.Build();
host.Run();Services are injected automatically through constructor injection:
public class MyController : ControllerBase
{
private readonly IDataService _dataService;
public MyController(IDataService dataService)
{
_dataService = dataService;
}
public async Task<IActionResult> Get()
{
var data = await _dataService.GetDataAsync();
return Ok(data);
}
}That's it! No manual service registration needed - EasyScrutor handles it all for you.
By default, AddEasyScrutor() scans all assemblies in your application's dependency context. For better performance, especially in large applications, you can filter which assemblies to scan:
builder.Services.AddEasyScrutor(assembly =>
{
// Only scan assemblies from your application, exclude framework assemblies
return !assembly.FullName?.StartsWith("Microsoft.", StringComparison.Ordinal) == true &&
!assembly.FullName?.StartsWith("System.", StringComparison.Ordinal) == true &&
!assembly.FullName?.StartsWith("netstandard", StringComparison.Ordinal) == true;
});Or target specific assemblies:
// Only scan assemblies matching your application name
builder.Services.AddEasyScrutor(assembly =>
assembly.FullName?.StartsWith("MyCompany.MyApp", StringComparison.Ordinal) == true);Or exclude test/development assemblies in production:
builder.Services.AddEasyScrutor(assembly =>
{
var name = assembly.FullName ?? string.Empty;
return !name.Contains("Tests") &&
!name.Contains("Mock") &&
!name.StartsWith("Microsoft.") &&
!name.StartsWith("System.");
});This can significantly improve application startup time by reducing the number of assemblies scanned for service registration.
See examples/README.md for runnable sample apps (Web API, MVC, Blazor Server, and a console/worker host).
We welcome contributions! Please read our CONTRIBUTING.md guide to get started.
See CHANGELOG.md for the complete version history and detailed list of changes since forking from Scrutor.AspNetCore.