【发布时间】:2020-07-11 22:07:52
【问题描述】:
我有一个使用 Entity Framework Core O/RM 的 ASP.NET Core MVC 应用程序。我正在我的应用程序中实现通用存储库模式。有两个实体,Employee 和 Department。有一个上下文类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.IGenericRepository
1[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.IGenericRepository
1[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