Fox LIFE 2024-08-16 17:17 采纳率: 72.7%
浏览 11

ASP.NET Core WEB API (Cntroller Api) 注册服务抛出错误

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'.)

补充截图:

img

功能预期是添加一组基于泛型仓储的服务,

我的项目内提供了如下结构的代码:


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

//其他命名规则一致,也都有不同的服务需要使用

这是什么问题

  • 是泛型仓储那部分注册失败引发的吗?
  • 还是后面一大串都是注册失败的?
  • 该怎么解决?
  • 需要提供更多代码吗?
  • 写回答

2条回答 默认 最新

  • GISer Liu 2024-08-16 17:20
    关注

    该回答引用自GPT-deepseek, 由博主 GISer Liu 编写:

    问题分析

    根据异常信息和代码分析,问题主要出在服务注册阶段。具体来说,异常信息表明在尝试激活某些服务时,无法解析依赖的服务类型。例如:

    Unable to resolve service for type 'TaskManangerSystem.Services.Repository.CategoryRepository' while attempting to activate 'TaskManangerSystem.Services.Mapper.ToCategoryConverter'.
    

    这表明在激活 ToCategoryConverter 时,无法解析 CategoryRepository 服务。类似的问题也出现在其他服务上。

    问题定位

    1. 服务注册不完整:在注册服务时,可能缺少某些必要的依赖服务注册。
    2. 依赖注入配置错误:在配置依赖注入时,可能存在错误的依赖关系或未正确注册依赖服务。

    解决方案

    1. 确保所有依赖服务已注册

    首先,确保所有依赖的服务都已正确注册。根据异常信息,需要检查并确保 CategoryRepository, InventoryRepository 等服务已注册。

    2. 修改服务注册代码

    在注册服务时,确保所有依赖的服务都已注册。以下是修改后的服务注册代码示例:

    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>()
    
        // 添加缺失的依赖服务注册
        .AddTransient<CategoryRepository>()
        .AddTransient<InventoryRepository>()
        .AddTransient<TaskAffairRepository>()
        .AddTransient<CategoryRepositoryAsync>()
        .AddTransient<InventoryRepositoryAsync>()
        .AddTransient<TaskAffairRepositoryAsync>();
    

    3. 修改 AutoMapper 转换器代码

    确保 AutoMapper 转换器代码中使用的依赖服务已正确注入。以下是修改后的 ToInventoryConverter 代码示例:

    public class ToInventoryConverter : ITypeConverter<InventoryForAddOrUpdate, InventoryInfo>
    {
        private readonly InventoryRepository _inventoryRepository;
        private readonly CategoryRepository _categoryRepository;
    
        public ToInventoryConverter(InventoryRepository inventoryRepository, CategoryRepository categoryRepository)
        {
            _inventoryRepository = inventoryRepository;
            _categoryRepository = categoryRepository;
        }
    
        public InventoryInfo Convert(InventoryForAddOrUpdate source, InventoryInfo destination, ResolutionContext context)
        {
            // 转换逻辑
        }
    }
    

    总结

    通过确保所有依赖服务已正确注册,并修改服务注册代码和 AutoMapper 转换器代码,可以解决服务注册抛出错误的问题。如果问题仍然存在,请检查其他相关代码,确保所有依赖关系和注入配置正确无误。

    如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑

    评论

报告相同问题?

问题事件

  • 创建了问题 8月16日

悬赏问题

  • ¥15 web前端开发怎么实现像图片这样的页面啊?
  • ¥15 ubuntu 20.04 网卡启用后,只有ipv6 没有 ipv4 无法上网
  • ¥15 QT任务管理器无法正确获取展开,有悬赏15元速求,如何解决?(相关搜索:标识符|结构体)
  • ¥15 使用delphi 10.3+intraweb 生成的页面怎么实现自动滚屏
  • ¥20 思科:Router c3600 MN-4E插槽
  • ¥15 16进制修改视频的元数据
  • ¥15 HTML中css的位置信息居然会导致元素大小发生变化
  • ¥15 岛津txt格式文件转nirs格式
  • ¥15 有偿指导软件编程与八股
  • ¥15 石墨烯磁表面等离子体