System.AggregateException in Program.cs in ASP.NET Core MVC

I have an ASP.NET Core MVC application which uses Entity Framework Core O/RM. I am implementing generic repository pattern in my application. There are two entities, Employee & Department. There is one context class EmployeeDepartmentDetails.There's an interface IGenericRepository which is implemented by GenericRepository. GenericRepository has a Dependency Injection of EmployeeDepartmentDetails.My Controller EmployeeController has a Dependency Injection of IGenericRepository.

IGenericRepository.cs -

public interface IGenericRepository<TEntity> where TEntity : class
{
    //...
}

GenericRepository.cs - Here EmployeeDepartmentDetails is my context class which inherits DBContext class-

public class GenericRepository<TEntity> :IGenericRepository<TEntity> where TEntity : class
{
    DbSet<TEntity> _dbSet;
    // Dependency Injection 
    private readonly EmployeeDepartmentDetail _employeeDepartmentDetail; 
    public GenericRepository(EmployeeDepartmentDetail employeeDepartmentDetail)
    {
        _employeeDepartmentDetail = employeeDepartmentDetail;
        _dbSet = _employeeDepartmentDetail.Set<TEntity>();
    } 

    //... 
}

Program.cs -

public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run(); // I am getting those two exceptions here
}

StartUp.cs -

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddScoped(typeof(IGenericRepository<Type>), typeof(GenericRepository<Type>)); 
}

EmployeeController.cs -

public class EmployeeController : Controller
{
    private readonly IGenericRepository<Employee> _genericRepository;
    public EmployeeController(IGenericRepository<Employee> genericRepository)
    {
        _genericRepository = genericRepository;
    } 

    //... 
}

I am getting two exception, when I am running my application -

System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: InfrastructureLayer.GenericRepository.Interface.IGenericRepository1[System.Type] Lifetime: Scoped ImplementationType: InfrastructureLayer.GenericRepository.Implementation.GenericRepository1[System.Type]': A suitable constructor for type 'InfrastructureLayer.GenericRepository.Implementation.GenericRepository1[System.Type]' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.) Source=Microsoft.Extensions.DependencyInjection StackTrace:at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at CrudEfCore.Program.Main(String[] args) in C:\Users\singh\Desktop\MVC\GenericRepository\CrudEfCore\Program.cs:line 16 This exception was originally thrown at this call stack:[External Code]

Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: InfrastructureLayer.GenericRepository.Interface.IGenericRepository1[System.Type] Lifetime: Scoped ImplementationType: InfrastructureLayer.GenericRepository.Implementation.GenericRepository1[System.Type]': A suitable constructor for type 'InfrastructureLayer.GenericRepository.Implementation.GenericRepository`1[System.Type]' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

Inner Exception 2: InvalidOperationException: A suitable constructor for type 'InfrastructureLayer.GenericRepository.Implementation.GenericRepository`1[System.Type]' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

I am unable to understand the exception, I don't know what I am doing wrong here. I know I have pasted lot of code, but I don't even know how to explain the exception.

1 answer

  • answered 2020-03-31 11:18 Nkosi

    This is the cause of the issue

    services.AddScoped(typeof(IGenericRepository<Type>), typeof(GenericRepository<Type>)); 
    

    Type is an abstract class and as the error message states:

    Ensure the type is concrete and services are registered for all parameters of a public constructor.

    note: emphasis mine

    Next Type is not what you should be using as the generic argument when registering the generic service

    In this case you can register using (generic) open types, eliminating the need to register every (generic) constructed type:

    services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>)); 
    

    Reference Dependency injection in ASP.NET Core