ASP.NET Core WEB API (Cntroller Api) 注册服务抛出错误
异常信息如下:
Unhandled exception. System.AggregateException: Some services are not able to be constructed
(Error while validating the service descriptor 'ServiceType: TaskManangerSystem.Services.Mapper.ToCategoryConverter Lifetime: Transient ImplementationType: TaskManangerSystem.Services.Mapper.ToCategoryConverter': Unable to resolve service for type 'TaskManangerSystem.Services.Repository.CategoryRepository' while attempting to activate 'TaskManangerSystem.Services.Mapper.ToCategoryConverter'.)
(Error while validating the service descriptor 'ServiceType: TaskManangerSystem.Services.Mapper.ToInventoryConverter Lifetime: Transient ImplementationType: TaskManangerSystem.Services.Mapper.ToInventoryConverter': Unable to resolve service for type 'TaskManangerSystem.Services.Repository.InventoryRepository' while attempting to activate 'TaskManangerSystem.Services.Mapper.ToInventoryConverter'.)
(Error while validating the service descriptor 'ServiceType: TaskManangerSystem.Services.Mapper.ToTaskAffairConverter Lifetime: Transient ImplementationType: TaskManangerSystem.Services.Mapper.ToTaskAffairConverter': Unable to resolve service for type 'TaskManangerSystem.Services.Repository.CategoryRepository' while attempting to activate 'TaskManangerSystem.Services.Mapper.ToTaskAffairConverter'.)
(Error while validating the service descriptor 'ServiceType: TaskManangerSystem.Services.Mapper.CategoryNameToGuidConverter Lifetime: Transient ImplementationType: TaskManangerSystem.Services.Mapper.CategoryNameToGuidConverter': Unable to resolve service for type 'TaskManangerSystem.Services.Repository.CategoryRepository' while attempting to activate 'TaskManangerSystem.Services.Mapper.CategoryNameToGuidConverter'.)
(Error while validating the service descriptor 'ServiceType: AutoMapper.ITypeConverter`2[TaskManangerSystem.Services.Info.CategoryForAddOrUpdate,TaskManangerSystem.Models.Category] Lifetime: Transient ImplementationType: TaskManangerSystem.Services.Mapper.ToCategoryConverter': Unable to resolve service for type 'TaskManangerSystem.Services.Repository.CategoryRepository' while attempting to activate 'TaskManangerSystem.Services.Mapper.ToCategoryConverter'.)
(Error while validating the service descriptor 'ServiceType: AutoMapper.ITypeConverter`2[TaskManangerSystem.Services.Info.InventoryForAddOrUpdate,TaskManangerSystem.Models.InventoryInfo] Lifetime: Transient ImplementationType: TaskManangerSystem.Services.Mapper.ToInventoryConverter': Unable to resolve service for type 'TaskManangerSystem.Services.Repository.InventoryRepository' while attempting to activate 'TaskManangerSystem.Services.Mapper.ToInventoryConverter'.)
(Error while validating the service descriptor 'ServiceType: AutoMapper.ITypeConverter`2[TaskManangerSystem.Services.Info.TaskAffairForAdd,TaskManangerSystem.Models.TaskAffair] Lifetime: Transient ImplementationType: TaskManangerSystem.Services.Mapper.ToTaskAffairConverter': Unable to resolve service for type 'TaskManangerSystem.Services.Repository.CategoryRepository' while attempting to activate 'TaskManangerSystem.Services.Mapper.ToTaskAffairConverter'.)
补充截图:
功能预期是添加一组基于泛型仓储的服务,
我的项目内提供了如下结构的代码:
namespace IRepository{
public interface IRepository<TSource>{}
public interface IRepositoryAsync<TSource>{}
public interface IIncludeRepository<TSource>{}
public interface IIncludeRepositoryAsync<TSource>{}
}
namespace RepositoryAbstract{
public class Repository<TSource>(DbContext storage) : IRepository<TSource> {}
public class RepositoryAsync<TSource>(DbContext storage) : IRepositoryAsync<TSource> {}
public class IncludeRepository<TSource>(DbContext storage, Expression<Func<TSource, object?>>[] attribute) : IIncludeRepository<TSource> {}
public class IncludeRepositoryAsync<TSource>(DbContext storage, Expression<Func<TSource, object?>>[] attribute) : IIncludeRepositoryAsync<TSource> {}
}
namespace RepositoryRealize{
public class EmployeeRepository(ManagementSystemContext storage) : Repository<Employee>(storage){}
public class EmployeeRepositoryAsync(ManagementSystemContext storage) : RepositoryAsync<Employee>(storage){}
//以及其他同格式实现的仓储类
public class TaskAffairRepository(ManagementSystemContext storage) : IncludeRepository<TaskAffair>(storage, DelegateExpressionTree.task){}
public class TaskAffairRepositoryAsync(ManagementSystemContext storage) : IncludeRepositoryAsync<TaskAffair>(storage, DelegateExpressionTree.task){}
}
注册服务的代码如下:
builder
.ConditionalCheck(e => e.Environment.IsDevelopment(), e => e.Services.AddSwaggerGen(), e => e.Services)
.AddDbContext<ManagementSystemContext>(p => p.UseMySQL(SystemInfo.DB_LINK))
.AddSingleton<IAuthorizationHandler, AuthHandler>()
.AddAutoMapper(Assembly.GetExecutingAssembly())
.AddSingleton<BearerBuilder>()
.AddTransient<ITypeConverter<EmployeeAccountForLoginOrAdd, Employee>, ToEmployeeConverter>()
.AddTransient<ITypeConverter<CategoryForAddOrUpdate, Category>, ToCategoryConverter>()
.AddTransient<ITypeConverter<InventoryForAddOrUpdate, InventoryInfo>, ToInventoryConverter>()
.AddTransient<ITypeConverter<TaskAffairForAdd, TaskAffair>, ToTaskAffairConverter>()
.AddTransient(typeof(IRepositoryAsync<>), typeof(RepositoryAsync<>))
.AddTransient(typeof(IRepository<>), typeof(Repository<>))
.AddTransient(typeof(IIncludeRepositoryAsync<>), typeof(IncludeRepositoryAsync<>))
.AddTransient(typeof(IIncludeRepository<>), typeof(IncludeRepository<>))
.AddTransient<IRepositoryAsync<Employee>, EmployeeRepositoryAsync>()
.AddTransient<IIncludeRepositoryAsync<Category>, CategoryRepositoryAsync>()
.AddTransient<IIncludeRepositoryAsync<InventoryInfo>, InventoryRepositoryAsync>()
.AddTransient<IIncludeRepositoryAsync<Customer>, CustomerRepositoryAsync>()
.AddTransient<IIncludeRepositoryAsync<TaskAffair>, TaskAffairRepositoryAsync>()
.AddTransient<IRepository<Employee>, EmployeeRepository>()
.AddTransient<IIncludeRepository<InventoryInfo>, InventoryRepository>()
.AddTransient<IIncludeRepository<Category>, CategoryRepository>()
.AddTransient<IIncludeRepository<Customer>, CustomerRepository>()
.AddTransient<IIncludeRepository<TaskAffair>, TaskAffairRepository>()
AutoMapper 的转换器代码:
public class ToInventoryConverter(InventoryRepository IRA, CategoryRepository CRA) : ITypeConverter<InventoryForAddOrUpdate, InventoryInfo>{}
//其他命名规则一致,也都有不同的服务需要使用
这是什么问题
- 是泛型仓储那部分注册失败引发的吗?
- 还是后面一大串都是注册失败的?
- 该怎么解决?
- 需要提供更多代码吗?