Scrutor not working to automatically register the services with the ASP.NET Core DI container in case of .NET 7 application

617 Views Asked by At

I have created a .NET 7 Web API project using the following links :

https://codewithmukesh.com/blog/modular-architecture-in-aspnet-core/

https://medium.com/design-microservices-architecture-with-patterns/microservices-killer-modular-monolithic-architecture-ac83814f6862

In this case I am following Modular Monolithic architecture to design the solution structure.

I have a Web API project in the Host folder which references Scrutor nuget package (Version:4.2.2) to automatically register the services with the ASP.NET Core DI container (Reference : https://andrewlock.net/using-scrutor-to-automatically-register-your-services-with-the-asp-net-core-di-container/)

Here goes the code:

AppDIExtensions.cs

public static class AppDIExtensions
    {
        public static IServiceCollection AddDependencies(this IServiceCollection services) =>
            services.Scan(scan => scan
                    .FromAssembliesOf(typeof(ICacheProvider))
                    .AddClasses(classes => classes.InNamespaces("Shared.Infrastructure.Services.Cloud"))
                    .AsImplementedInterfaces()
                    .WithTransientLifetime()
                );
    }

In the above ICacheProvider is located with the Shared.Core.Abstractions.Contracts.Cloud (Part of Shared folder).

AddDependencies method is referenced from the Program.cs of API project which has reference to Module.Catalog and Shared.Infrastructure project.

Program.cs

var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;

// Add AzureKeyVault
builder.Configuration.AddAzureKeyVaultConfiguration(config);
builder.Services.AddDependencies();

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddCatalogModule(config);
builder.Services.AddSharedInfrastructure(config);
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Solution Structure:

enter image description here

Can anyone help me here with some code sample which will serve as a reference for my implementation

1

There are 1 best solutions below

0
santosh kumar patro On

I was able to fix the issue with the following code :

public static IServiceCollection AddDependencies(this IServiceCollection services) =>
            services.Scan(scan => scan
                    .FromApplicationDependencies()
                    .AddClasses(classes => classes.InNamespaces("Shared.Infrastructure.Services.Cloud"))
                    .UsingRegistrationStrategy(RegistrationStrategy.Skip)
                    .AsImplementedInterfaces()
                    .WithTransientLifetime()
                );

In this case I used FromApplicationDependencies() method instead of FromAssembliesOf()