Assembly scanning with Scrutor

By | November 28, 2019

Scrutor is a fantastic library, which fills two gaps in the service registration functionality available in the Microsoft container by providing support for assembly scanning and using the decorator pattern

One of the features of Scrutor is providing assembly scanning supportOne of the features of Scrutor is providing assembly scanning support

public static class SampleServiceCollectionExtensions
{
    public static IServiceCollection AddSampleRules(this IServiceCollection services)
    {
        // Scrutor assembly scanning
        services.Scan(scan => scan
            .FromAssemblyOf<ISampleRule>()
            .AddClasses(classes => classes.AssignableTo<IScopedSampleRule>())
            .AsImplementedInterfaces()
            .WithSingletonLifetime());
        return services;
    }
}

The Scrutor assembly scanning code is only slightly shorter than the manual code. So what’s the advantage of using assembly scanning here?

For a few rules, manually registering them is not a problem. But as the complexity grows and more rules need to be added, it’s entirely possible to forget to add the required registrations for them.


Assembly scanning in this way means that I can add a new implementation of the ICourtBookingRule at any time in the future, and at runtime it will be found by the scanning process and registered for me.


Ultimately, this reduces risk and manual work needed to maintain all of the registrations.

One thought on “Assembly scanning with Scrutor

Leave a Reply

Your email address will not be published. Required fields are marked *