Scrutor for C# (.NET): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Scrutor for C# (.NET): Complete Guide with Examples

\n\n

Scrutor is an open-source library for the .NET platform that extends the built-in Dependency Injection (DI) mechanism with automatic assembly scanning capabilities. It allows registering services in the DI container based on naming conventions, attributes, or interfaces, which significantly simplifies IoC container configuration in large projects.

\n\n

Why is this library needed? In the standard Microsoft.Extensions.DependencyInjection container, each service must be registered manually: services.AddScoped<IMyService, MyService>(). In a project with dozens or hundreds of services, this leads to code bloat in Startup.cs or Program.cs, reduces readability, and increases the risk of errors. Scrutor solves this problem by allowing you to register all services from an assembly with a single line: services.Scan(scan => scan.FromAssemblyOf<IMyService>()...).

\n\n

Scrutor is especially useful in architectures that use Domain-Driven Design (DDD), Clean Architecture, or modular monoliths. It automatically finds all interface implementations, classes with specific attributes (e.g., [ServiceDescriptor]), or classes matching certain naming patterns (e.g., all classes ending with "Repository"). This makes the code cleaner and adding new services a trivial task that does not require changing the DI configuration.

\n\n

Installation

\n\n

Scrutor is installed via NuGet Package Manager. Run one of the following commands:

\n\n
# Via .NET CLI\ndotnet add package Scrutor\n\n# Via Package Manager Console (Visual Studio)\nInstall-Package Scrutor
\n\n

After installation, a reference to the Scrutor library will be added to the project. Make sure you have .NET SDK version 6.0 or higher installed (recommended .NET 8.0+).

\n\n

Quick Start

\n\n

Let's look at a minimal working example. Let's create a console application with three files: the IGreetingService interface, its implementation GreetingService, and the entry point Program.cs.

\n\n
// IGreetingService.cs\npublic interface IGreetingService\n{\n    string Greet(string name);\n}\n\n// GreetingService.cs\npublic class GreetingService : IGreetingService\n{\n    public string Greet(string name) => $"Hello, {name}!";\n}\n\n// Program.cs\nusing Microsoft.Extensions.DependencyInjection;\nusing Scrutor;\n\nvar services = new ServiceCollection();\n\n// Manual registration (standard approach)\n// services.AddTransient<IGreetingService, GreetingService>();\n\n// Registration via Scrutor (assembly scanning)\nservices.Scan(scan => scan\n    .FromAssemblyOf<IGreetingService>() // Scan the assembly containing IGreetingService\n    .AddClasses(classes => classes.AssignableTo<IGreetingService>()) // Find classes implementing IGreetingService\n    .AsImplementedInterfaces() // Register as interface implementation\n    .WithTransientLifetime()); // Set Transient lifetime\n\nvar serviceProvider = services.BuildServiceProvider();\nvar greetingService = serviceProvider.GetRequiredService<IGreetingService>();\nConsole.WriteLine(greetingService.Greet("World"));\n// Output: Hello, World!
\n\n

In this example, Scrutor automatically finds the GreetingService class in the same assembly as the IGreetingService interface and registers it as Transient. If you add a new implementation of IGreetingService, it will be registered automatically without changing the Program.cs code.

\n\n

Main Methods and Classes

\n\n

Scrutor provides a fluent API for building call chains. Let's look at 8 key methods/classes with signatures, descriptions, and examples.

\n\n

1. Scan(IServiceCollect

Recommendations