Scrutor 6.1.0
Scrutor

Scrutor - I search or examine thoroughly; I probe, investigate or scrutinize
From scrūta, as the original sense of the verb was to search through trash. - https://en.wiktionary.org/wiki/scrutor
Assembly scanning and decoration extensions for Microsoft.Extensions.DependencyInjection
Installation
Install the Scrutor NuGet Package.
Package Manager Console
Install-Package Scrutor
.NET Core CLI
dotnet add package Scrutor
Usage
The library adds two extension methods to IServiceCollection:
Scan- This is the entry point to set up your assembly scanning.Decorate- This method is used to decorate already registered services.
See Examples below for usage examples.
Examples
Scanning
var collection = new ServiceCollection();
collection.Scan(scan => scan
// We start out with all types in the assembly of ITransientService
.FromAssemblyOf<ITransientService>()
// AddClasses starts out with all public, non-abstract types in this assembly.
// These types are then filtered by the delegate passed to the method.
// In this case, we filter out only the classes that are assignable to ITransientService.
.AddClasses(classes => classes.AssignableTo<ITransientService>())
// We then specify what type we want to register these classes as.
// In this case, we want to register the types as all of its implemented interfaces.
// So if a type implements 3 interfaces; A, B, C, we'd end up with three separate registrations.
.AsImplementedInterfaces()
// And lastly, we specify the lifetime of these registrations.
.WithTransientLifetime()
// Here we start again, with a new full set of classes from the assembly above.
// This time, filtering out only the classes assignable to IScopedService.
.AddClasses(classes => classes.AssignableTo<IScopedService>())
// Now, we just want to register these types as a single interface, IScopedService.
.As<IScopedService>()
// And again, just specify the lifetime.
.WithScopedLifetime()
// Generic interfaces are also supported too, e.g. public interface IOpenGeneric<T>
.AddClasses(classes => classes.AssignableTo(typeof(IOpenGeneric<>)))
.AsImplementedInterfaces()
// And you scan generics with multiple type parameters too
// e.g. public interface IQueryHandler<TQuery, TResult>
.AddClasses(classes => classes.AssignableTo(typeof(IQueryHandler<,>)))
.AsImplementedInterfaces());
Scanning compiled view (UI) types
By default, Scrutor excludes compiler-generated types from the .AddClasses() type filters. When loading views from a framework such as Avalonia UI, we need to opt in to compiler-generated types, like this:
.AddClasses(classes => classes
// Opt-in to compiler-generated types
.WithAttribute<CompilerGeneratedAttribute>()
// Optionally filter types to reduce number of service registrations.
.InNamespaces("MyApp.Desktop.Views")
.AssignableToAny(
typeof(Window),
typeof(UserControl)
)
.AsSelf()
.WithSingletonLifetime()
With some UI frameworks, these compiler-generated views implement quite a few interfaces, so unless you need them, it's probably best to register these classes .AsSelf(); in other words, be very precise with your filters that accept compiler generated types.
Decoration
var collection = new ServiceCollection();
// First, add our service to the collection.
collection.AddSingleton<IDecoratedService, Decorated>();
// Then, decorate Decorated with the Decorator type.
collection.Decorate<IDecoratedService, Decorator>();
// Finally, decorate Decorator with the OtherDecorator type.
// As you can see, OtherDecorator requires a separate service, IService. We can get that from the provider argument.
collection.Decorate<IDecoratedService>((inner, provider) => new OtherDecorator(inner, provider.GetRequiredService<IService>()));
var serviceProvider = collection.BuildServiceProvider();
// When we resolve the IDecoratedService service, we'll get the following structure:
// OtherDecorator -> Decorator -> Decorated
var instance = serviceProvider.GetRequiredService<IDecoratedService>();
Showing the top 20 packages that depend on Scrutor.
| Packages | Downloads |
|---|---|
|
Finsa.CaravanCore
Package Description
|
60 |
|
Finsa.CaravanCore
Package Description
|
72 |
|
Finsa.CaravanCore
Package Description
|
73 |
|
Finsa.CaravanCore
Package Description
|
80 |
|
Finsa.CaravanCore
Package Description
|
94 |
|
Finsa.CaravanCore
Package Description
|
105 |
|
Finsa.CaravanCore
Package Description
|
108 |
|
Finsa.CaravanCore
Package Description
|
112 |
|
Finsa.CaravanCore
Package Description
|
114 |
|
Finsa.CaravanCore
Package Description
|
160 |
|
Finsa.CaravanCore
Package Description
|
185 |
|
Finsa.CaravanCore
Package Description
|
297 |
|
Finsa.CaravanCore
Package Description
|
391 |
|
Finsa.CaravanCore
Package Description
|
441 |
|
Finsa.CaravanCore
Package Description
|
480 |
|
Finsa.CaravanCore
Package Description
|
580 |
|
Finsa.CaravanCore
Package Description
|
1,267 |
|
Finsa.CaravanCore
Package Description
|
2,171 |
|
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
|
83 |
|
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
|
430 |
.NET Framework 4.6.2
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.DependencyModel (>= 8.0.2)
.NET 8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.DependencyModel (>= 8.0.2)
.NET Standard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.DependencyModel (>= 8.0.2)
| Version | Downloads | Last updated |
|---|---|---|
| 6.1.0 | 13 | 06/06/2025 |
| 6.0.1 | 22 | 01/29/2025 |
| 6.0.0 | 20 | 02/14/2025 |
| 5.1.2 | 20 | 02/15/2025 |
| 5.1.1 | 21 | 01/30/2025 |
| 5.1.0 | 22 | 01/01/2025 |
| 5.0.3 | 20 | 01/01/2025 |
| 5.0.2 | 21 | 11/15/2024 |
| 5.0.1 | 31 | 09/29/2024 |
| 5.0.0 | 26 | 09/29/2024 |
| 4.2.2 | 35 | 02/06/2024 |
| 4.2.1 | 33 | 02/13/2023 |
| 4.2.0 | 75 | 06/18/2022 |
| 4.1.0 | 201 | 03/08/2022 |
| 4.0.0 | 31 | 02/18/2022 |
| 3.3.0 | 792 | 11/03/2020 |
| 3.2.2 | 253 | 10/25/2020 |
| 3.2.1 | 34 | 02/14/2024 |
| 3.2.0 | 35 | 02/14/2024 |
| 3.1.0 | 35 | 02/14/2024 |
| 3.0.3 | 28 | 02/14/2024 |
| 3.0.2 | 31 | 02/14/2024 |
| 3.0.1 | 29 | 02/14/2024 |
| 2.2.2 | 40 | 02/14/2024 |
| 2.2.1 | 35 | 02/14/2024 |
| 2.2.0 | 33 | 02/14/2024 |
| 2.1.2 | 32 | 02/14/2024 |
| 2.1.1 | 31 | 02/14/2024 |
| 2.1.0 | 30 | 02/14/2024 |
| 2.0.0 | 32 | 02/14/2024 |
| 2.0.0-rc2 | 25 | 02/14/2024 |
| 2.0.0-rc | 43 | 02/14/2024 |
| 2.0.0-beta | 27 | 02/14/2024 |
| 1.12.0 | 29 | 02/14/2024 |
| 1.11.0 | 30 | 02/14/2024 |
| 1.10.1 | 31 | 02/14/2024 |
| 1.10.0 | 25 | 02/14/2024 |
| 1.9.1 | 32 | 02/14/2024 |
| 1.9.0 | 27 | 02/14/2024 |
| 1.8.0 | 30 | 02/14/2024 |
| 1.7.0 | 26 | 02/14/2024 |
| 1.6.0 | 31 | 02/14/2024 |
| 1.5.0 | 31 | 02/14/2024 |
| 1.4.0 | 32 | 02/14/2024 |
| 1.3.2 | 27 | 02/14/2024 |
| 1.3.1 | 34 | 02/14/2024 |
| 1.3.0 | 29 | 02/14/2024 |
| 1.2.0 | 32 | 02/14/2024 |
| 1.1.1 | 34 | 02/14/2024 |
| 1.1.0 | 27 | 02/14/2024 |
| 1.0.0 | 32 | 02/14/2024 |