This directory contains example projects demonstrating how to use EasyScrutor for automatic dependency injection registration in different ASP.NET Core application types.
EasyScrutor is a dependency injection helper package that automatically scans and registers services based on marker interfaces, eliminating the need for manual service registration in Program.cs.
Instead of manually registering services like this:
builder.Services.AddScoped<IMyService, MyService>();
builder.Services.AddSingleton<IAnotherService, AnotherService>();You simply:
- Implement marker interfaces on your services (
IScopedLifetime,ISingletonLifetime,ITransientLifetime) - Call
AddEasyScrutor()once - All services are automatically discovered and registered!
A minimal Web API demonstrating:
- Auto-registration of scoped and singleton services
- Service injection in minimal API endpoints
- Custom endpoint using injected services
Run: dotnet run --project WebApiExample
Test: Navigate to /greeting/YourName to see auto-registered services in action
An MVC application showing:
- Auto-registration of transient services
- Service injection in MVC controllers
- Using services in views
Run: dotnet run --project MvcExample
View: Home page displays message from auto-registered service
A Blazor Server app demonstrating:
- Auto-registration of singleton services
- Service injection in Blazor components
- Shared state across users
Run: dotnet run --project BlazorServerExample
Try: Counter page uses a singleton service shared across all sessions
A generic host worker service (console application) demonstrating:
- Auto-registration in non-web applications
- Background worker using auto-registered services
- Singleton metrics collector shared across the application
- Scoped data processor
Run: dotnet run --project ConsoleHostExample
See: Console output showing auto-registered services in action
// Interface
public interface IMyService
{
string DoSomething();
}
// Implementation - Add the appropriate lifetime marker interface
public class MyService : IMyService, IScopedLifetime
{
public string DoSomething() => "Hello from auto-registered service!";
}For Web Applications (API, MVC, Blazor):
var builder = WebApplication.CreateBuilder(args);
// Add all your other services...
builder.Services.AddControllers();
// Add EasyScrutor - This scans and registers all services
builder.Services.AddEasyScrutor();
var app = builder.Build();
app.Run();For Generic Host / Console Applications:
var builder = Host.CreateApplicationBuilder(args);
// Add EasyScrutor - This scans and registers all services
builder.Services.AddEasyScrutor();
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
host.Run();public class MyController : Controller
{
private readonly IMyService _myService;
public MyController(IMyService myService)
{
_myService = myService; // Automatically injected!
}
}| Interface | Lifetime | Use Case |
|---|---|---|
IScopedLifetime |
Scoped | Services that should live for the duration of a request |
ISingletonLifetime |
Singleton | Services that should be created once and shared across the app |
ITransientLifetime |
Transient | Services that should be created new each time they're requested |
ISelfScopedLifetime |
Scoped | Register the service as itself (not interface) with scoped lifetime |
ISelfSingletonLifetime |
Singleton | Register the service as itself (not interface) with singleton lifetime |
ISelfTransientLifetime |
Transient | Register the service as itself (not interface) with transient lifetime |
✅ Cleaner Code - No more cluttered Program.cs with dozens of service registrations
✅ Convention-Based - Simply implement an interface to define the lifetime
✅ Type-Safe - Compile-time checked, no magic strings
✅ Maintainable - Services declare their own lifetime alongside their implementation
✅ Flexible - Can still manually register services when needed
✅ Discoverable - Easy to find all services by searching for lifetime interfaces
Build all examples:
dotnet buildRun a specific example:
dotnet run --project WebApiExample
dotnet run --project MvcExample
dotnet run --project BlazorServerExample
dotnet run --project ConsoleHostExampleFor more information, visit the EasyScrutor GitHub repository.
- Check the namespace: Services must be in a namespace that's scanned by
AddEasyScrutor() - Verify interface implementation: Ensure your service implements one of the lifetime marker interfaces
- Public classes only: Services must be public to be discovered
- Check assembly: By default, the entry assembly is scanned
- Check the examples for working code
- Open an issue if you find a bug
- Start a discussion for questions