Search code examples
asp.net-corescrutor

Using AddClasses more than once in Scrutor


I am trying to use Scrutor to ease DI registration in an asp.net core 3.1 application like below

services.Scan(scan => scan
                .FromAssembliesOf(typeof(IDataAccess),
                    typeof(IService))
                 .AddClasses(classes => classes.InNamespaces("MyNamespace1", 
                 "MyNamespace2",
                 "MyNamespace3"))
                 .AddClasses(classess => classess.AssignableTo<IMyInterface1>())
                .UsingRegistrationStrategy(RegistrationStrategy.Skip)
                .AsImplementedInterfaces()
                .WithTransientLifetime());

Unfortunately, the second AddClasses() kind of overwrite the the first one. Is there anyway, I can use both conventions together to detect classes?


Solution

  • You can try this code:

    services.Scan(scan => scan
                .FromAssembliesOf(typeof(IDataAccess),
                    typeof(IService))
                 .AddClasses(classes => classes.InNamespaces("MyNamespace1", 
                 "MyNamespace2",
                 "MyNamespace3"))
                 .AsImplementedInterfaces()
                 .WithTransientLifetime()
    
                .AddClasses(classess => classess.AssignableTo<IMyInterface1>())
                .UsingRegistrationStrategy(RegistrationStrategy.Skip)
                .AsImplementedInterfaces()
                .WithScopedLifetime());