Scrutor 7.0.0 (original) (raw)

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:

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>();

Entity Framework Extensions and Dapper Plus are major sponsors and proud to contribute to the development of Scrutor.

Entity Framework Extensions

Dapper Plus

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed.
.NET Core netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed.
.NET Standard netstandard2.0 is compatible. netstandard2.1 was computed.
.NET Framework net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed.
MonoAndroid monoandroid was computed.
MonoMac monomac was computed.
MonoTouch monotouch was computed.
Tizen tizen40 was computed. tizen60 was computed.
Xamarin.iOS xamarinios was computed.
Xamarin.Mac xamarinmac was computed.
Xamarin.TVOS xamarintvos was computed.
Xamarin.WatchOS xamarinwatchos was computed.

NuGet packages (994)

Showing the top 5 NuGet packages that depend on Scrutor:

Package Downloads
Swashbuckle.AspNetCore.Filters Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples. 109.0M
Scrutor.AspNetCore Dependency Injection Helper Package For .Net Core Apps 5.7M
Elsa.Core Elsa is a set of workflow libraries and tools that enable lean and mean workflowing capabilities in any .NET Core application. This package contains the core of Elsa. Tip: reference the `Elsa` package instead of this one. 4.1M
Sitko.Core.App Sitko.Core is a set of libraries to help build .NET Core applications fast 3.7M
Convey Convey 2.2M

GitHub repositories (92)

Showing the top 20 popular GitHub repositories that depend on Scrutor:

Repository Stars
LuckyPennySoftware/MediatR Simple, unambitious mediator implementation in .NET 11.8K
RayWangQvQ/BiliBiliToolPro B 站(bilibili)自动任务工具,支持docker、青龙、k8s等多种部署方式。全面拥抱AI。敏感肌也能用。 8.7K
elsa-workflows/elsa-core The Workflow Engine for .NET 7.8K
kurrent-io/KurrentDB KurrentDB is a database that's engineered for modern software applications and event-driven architectures. Its event-native design simplifies data modeling and preserves data integrity while the integrated streaming engine solves distributed messaging challenges and ensures data consistency. 5.8K
MapsterMapper/Mapster A fast, fun and stimulating object to object Mapper 5.1K
ivanpaulovich/clean-architecture-manga :cyclone: Clean Architecture with .NET6, C#10 and React+Redux. Use cases as central organizing structure, completely testable, decoupled from frameworks 4.3K
oskardudycz/EventSourcing.NetCore Examples and Tutorials of Event Sourcing in .NET 3.7K
aspnetrun/run-aspnetcore-microservices Microservices on .NET platforms used ASP.NET Web API, Docker, RabbitMQ, MassTransit, Grpc, Yarp API Gateway, PostgreSQL, Redis, SQLite, SqlServer, Marten, Entity Framework Core, CQRS, MediatR, DDD, Vertical and Clean Architecture implementation with using latest features of .NET 8 and C# 12 3.2K
vietnam-devs/coolstore-microservices A full-stack .NET microservices build on Dapr and Tye 2.5K
microsoft/sbom-tool The SBOM tool is a highly scalable and enterprise ready tool to create SPDX 2.2 compatible SBOMs for any variety of artifacts. 2.0K
lookup-foundation/RevitLookup Interactive Revit RFA and RVT project database exploration tool to view and navigate BIM element parameters, properties and relationships. 1.4K
meysamhadeli/booking-microservices A practical microservices with the latest technologies and architectures like Vertical Slice Architecture, Event Sourcing, CQRS, DDD, gRpc, MongoDB, RabbitMq, Masstransit, and Aspire in .Net 10. 1.4K
thangchung/clean-architecture-dotnet 🕸 Yet Another .NET Clean Architecture, but for Microservices project. It uses Minimal Clean Architecture with DDD-lite, CQRS-lite, and just enough Cloud-native patterns apply on the simple eCommerce sample and run on Tye with Dapr extension 🍻 1.3K
grandnode/grandnode2 E-commerce platform built with ASP.NET Core using MongoDB for NoSQL storage 1.3K
roflmuffin/CounterStrikeSharp CounterStrikeSharp allows you to write server plugins in C# for Counter-Strike 2/Source2/CS2 1.3K
matthewrenze/clean-architecture-demo A sample app for my online course "Clean Architecture: Patterns, Practices, and Principles" 1.2K
gautema/CQRSlite A lightweight framework to help creating CQRS and Eventsourcing applications in C# 1.1K
mehdihadeli/food-delivery-microservices 🍔 A practical and cloud-native food delivery microservices, built with .Net Aspire, .Net 9, MassTransit, Domain-Driven Design, CQRS, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies. 995
CodeMazeBlog/CodeMazeGuides The main repository for all the Code Maze guides 979
snatch-dev/Convey A simple recipe for .NET Core microservices. 958

Include prerelease

Include vulnerable

Include deprecated

Version Downloads Last Updated
7.0.0 6,704,989 11/24/2025
6.1.0 12,215,393 6/3/2025
6.0.1 12,505,152 1/14/2025
6.0.0 270,261 1/13/2025
5.1.2 997,615 1/13/2025
5.0.3 164,488 12/23/2024
5.0.2 4,616,878 11/7/2024
5.0.1 5,609,070 9/26/2024
5.0.0 201,582 9/25/2024
4.2.2 39,634,953 3/27/2023
4.2.1 3,643,883 2/8/2023
4.2.0 17,602,196 5/31/2022
4.1.0 6,832,107 3/2/2022
4.0.0 2,123,654 1/28/2022
3.3.0 108,133,449 11/3/2020
3.2.2 5,704,554 8/25/2020
3.2.1 6,341,356 4/20/2020
3.2.0 2,425,168 2/6/2020