[英]System.AggregateException on GetStringAsync in asp.net
[英]System.AggregateException in Program.cs in ASP.NET Core MVC
我有一个使用 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>();
}
//...
}
程序.cs -
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run(); // I am getting those two exceptions here
}
启动.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 HResult=0x80131500 消息=无法构建某些服务(验证服务描述符“ServiceType:InfrastructureLayer.GenericRepository.Interface.IGenericRepository
1[System.Type] Lifetime: Scoped ImplementationType: InfrastructureLayer.GenericRepository.Implementation.GenericRepository
出错1[System.Type] Lifetime: Scoped ImplementationType: InfrastructureLayer.GenericRepository.Implementation.GenericRepository
1[System.Type]':1[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(IEnumerable
适合类型“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(IEnumerable
构造函数1[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(IEnumerable
1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceOptions options) at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(1[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(IEnumerable
1 serviceDescriptors, ServiceProviderOptions options)扩展.依赖 encyInjection.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( ) 在 C:\\Users\\singh\\Desktop\\MVC\\GenericRepository\\CrudEfCore\\Program.cs:line 16 中的 CrudEfCore.Program.Main(String[] args) 这个异常最初是在这个调用堆栈中抛出的:[外部代码]内部异常 1:InvalidOperationException:验证服务描述符时出错“ServiceType: InfrastructureLayer.GenericRepository.Interface.IGenericRepository
1[System.Type] Lifetime: Scoped ImplementationType: InfrastructureLayer.GenericRepository.Implementation.GenericRepository
1[System.Type]”:一个合适的无法找到类型“InfrastructureLayer.GenericRepository.Implementation.GenericRepository`1[System.Type]”的构造函数。 确保类型是具体的,并且为公共构造函数的所有参数注册了服务。内部异常 2:InvalidOperationException:找不到适合类型“InfrastructureLayer.GenericRepository.Implementation.GenericRepository`1[System.Type]”的构造函数。 确保类型是具体的,并且为公共构造函数的所有参数注册了服务。
我无法理解异常,我不知道我在这里做错了什么。 我知道我粘贴了很多代码,但我什至不知道如何解释异常。
这是问题的原因
services.AddScoped(typeof(IGenericRepository<Type>), typeof(GenericRepository<Type>));
Type
是一个抽象类,如错误消息所述:
确保类型是具体的,并且为公共构造函数的所有参数注册了服务。
注意:强调我的
Next Type
不是您在注册通用服务时应该使用的通用参数
在这种情况下,您可以使用(通用)开放类型进行注册,无需注册每个(通用)构造类型:
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.