Home > Blockchain >  Resolve and Register services using Scrutor in Asp.net core
Resolve and Register services using Scrutor in Asp.net core

Time:11-22

i have an interface(IDomainService) and a (lot) like it in my app which i mark more interfaces with it(IProductionLineTitleDuplicationChecker ) like what u will see in the rest:

public interface IDomainService
    {

    }
public interface IProductionLineTitleDuplicationChecker : IDomainService
    {
        ///
    }

and the implementation like this:

public class ProductionLineTitleDuplicationChecker : IProductionLineTitleDuplicationChecker
    {
        private readonly IProductionLineRepository _productionLineRepository;

        public ProductionLineTitleDuplicationChecker(IProductionLineRepository productionLineRepository)
        {
            _productionLineRepository = productionLineRepository;
        }

        public bool IsDuplicated(string productionLineTitle)
        {
            ///
        }
    }

right now im using the built-in DI-container to resolve and register the services but i want to change it and use scrutor instead

how can i resolve and register my Services using scrutor?

CodePudding user response:

I think your situation is consistent with this post, please try to use the way inside:

services.Scan(scan => scan
    .FromAssemblyOf<IProductionLineTitleDuplicationChecker>()
    .AddClasses(classes => classes
        .AssignableTo<IProductionLineTitleDuplicationChecker>())
.AsImplementedInterfaces()
.WithScopedLifetime());

CodePudding user response:

You could just leverage the Scrutor extension methods for Microsoft.Extensions.DependencyInjection.IServiceCollection. In your Startup.cs:

public void ConfigureServices(IServiceCollection serviceCollection)
{
    serviceCollection
        .Scan(x => x.FromAssemblyOf<ProductionLineTitleDuplicationChecker>()
            .AddClasses()
            .AsImplementedInterfaces()
            .WithTransientLifetime());
}
  • Related