某些服务无法构建。验证服务描述符时出错

Some services are not able to be constructed. Error while validating the service descriptor

完整的错误正文

System.AggregateException: 'Some services are not able to be constructed 
(Error while validating the service descriptor 'ServiceType: WebApp.Application.Common.Interfaces.IUserService
Lifetime: Transient ImplementationType: WebApp.Application.Common.Services.UserService':
Unable to resolve service for type 'WebApp.Application.Common.Interfaces.ISettingsService'
while attempting to activate 'WebApp.Application.Common.Services.UserService'.)'

我让 UserService 负责用户创建(数据库模型用户),我需要从 SettingsService(数据库模型 NotificationSettings、PersonalSettings)调用 AddSettings 来为新注册的用户创建设置对象。目前,我正在尝试通过 UserService 的 AddUser 方法来完成:

public void AddUser(AddUserDTO addUserDTO)
{
   try
   {
        userRepository.AddAsync(mapper.Map<AddUserDTO, User>(addUserDTO));
        var user = userRepository.FindAsync(a => a.FullName == addUserDTO.FullName && a.Email == addUserDTO.Email);
        settingsService.AddSettings(user.Id);
   }
   catch (Exception ex)
   {
       logger.LogError(ex.Message);
       throw;
   }
}

AddSettings 方法:

public async void AddSettings(int id)
{
    try
    {
        notificationSettingsRepository.Add(new NotificationSettings
        {
            UserId = id,
            PushNewFollowerNotify = false,
            PushNewMessageNotify = false,
            PushLikeNotify = false
        });
        personalSettingsRepository.Add(new PersonalSettings
        {
            UserId = id,
            FollowerSeeFollowers = false,
            FollowerSeeSavedRecipe = false,
        });
    }
    catch (Exception ex)
    {
        logger.LogError(ex.Message);
    }
}

用户服务构造函数:

private readonly IUserRepository userRepository;
private readonly ISettingsService settingsService;
private readonly ILogger<UserService> logger;
private readonly IMapper mapper;

public UserService(IUserRepository userRepository, ISettingsService settingsService, ILogger<UserService> logger, IMapper mapper)
{
    this.userRepository = userRepository;
    this.settingsService = settingsService;
    this.logger = logger;
    this.mapper = mapper;
}

并且这两个服务都在 Startup.cs

中注册
services.AddTransient<ISearchService, SearchService>();
services.AddTransient<IUserService, UserService>();

我认为问题出在我如何将 SettingsService 注入到 UserService 中,但我找不到任何相关信息。

您应该注册 ISettingsService 服务:

services.AddTransient<ISettingsService, SettingsService>();

在您的示例中,您应该注册一个实现 ISettingsService 接口的 class,例如实现 IUserRepository 接口

UserRepository class