【问题标题】:System.AggregateException in Program.cs in ASP.NET Core MVCASP.NET Core MVC 中 Program.cs 中的 System.AggregateException
【发布时间】:2020-07-11 22:07:52
【问题描述】:

我有一个使用 Entity Framework Core O/RM 的 ASP.NET Core MVC 应用程序。我正在我的应用程序中实现通用存储库模式。有两个实体,EmployeeDepartment。有一个上下文类EmployeeDepartmentDetails。有一个接口IGenericRepository,由GenericRepository 实现。 GenericRepository 的依赖注入为 EmployeeDepartmentDetails。我的控制器 EmployeeController 的依赖注入为 IGenericRepository

IGenericRepository.cs -

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

GenericRepository.cs - 这里EmployeeDepartmentDetails 是我的上下文类,它继承了DBContext 类-

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;
    } 

    //... 
}

当我运行我的应用程序时,我遇到了两个异常 -

System.AggregateException H结果=0x80131500 消息=无法构造某些服务(验证服务描述符“ServiceType:InfrastructureLayer.GenericRepository.Interface.IGenericRepository1[System.Type] Lifetime: Scoped ImplementationType: InfrastructureLayer.GenericRepository.Implementation.GenericRepository1[System.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) 在 Microsoft.Extensions.Hosting.Internal 的 Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) 在 Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)。 ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder) 在 Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() 在 Microsoft.Extensions.Hosting.HostBuilder.Build() 在 CrudEfCore.Program.Main(String[] args) 在 C:\Users \singh\Desktop\MVC\GenericRepository \CrudEfCore\Program.cs:第 16 行 这个异常最初是在这个调用栈中抛出的:[外部代码]

内部异常 1: InvalidOperationException:验证服务描述符“ServiceType:InfrastructureLayer.GenericRepository.Interface.IGenericRepository1[System.Type] Lifetime: Scoped ImplementationType: InfrastructureLayer.GenericRepository.Implementation.GenericRepository1[System.Type]”时出错:适用于“InfrastructureLayer.GenericRepository.Implementation.GenericRepository`1[System.Type]”类型的构造函数无法定位。确保类型是具体的,并且为公共构造函数的所有参数注册了服务。

内部异常 2: InvalidOperationException:找不到适合类型“InfrastructureLayer.GenericRepository.Implementation.GenericRepository`1[System.Type]”的构造函数。确保类型是具体的,并且为公共构造函数的所有参数注册了服务。

我无法理解异常,我不知道我在这里做错了什么。我知道我已经粘贴了很多代码,但我什至不知道如何解释异常。

【问题讨论】:

    标签: c# dependency-injection entity-framework-core asp.net-core-mvc


    【解决方案1】:

    这是问题的原因

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

    Type 是一个抽象类,如错误消息所述:

    确保类型为具体,并且为公共构造函数的所有参数注册服务。

    注意:强调我的

    下一个Type 不是您在注册通用服务时应该使用的通用参数

    在这种情况下,您可以使用(通用)开放类型进行注册,无需注册每个(通用)构造类型:

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

    参考Dependency injection in ASP.NET Core

    【讨论】:

    • 成功了。谢谢你。你能解释一下你的答案吗?我还是不明白这个问题。
    • 嗨 Nihal,要更好地理解使用泛型,this 将是一个不错的起点。
    猜你喜欢
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2022-06-13
    • 2018-02-01
    相关资源
    最近更新 更多