Using AddClasses more than once in Scrutor

325 Views Asked by At

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?

1

There are 1 best solutions below

1
Yinqiu On BEST ANSWER

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