Home > Software design >  Some services are not able to be constructed. Error while validating the service descriptor
Some services are not able to be constructed. Error while validating the service descriptor

Time:09-16

Full error body

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

I have UserService responsible for the user creation (DB model User) and I need to call AddSettings from the SettingsService (DB models NotificationSettings, PersonalSettings) to create Settings objects for the newly registered user. Currently, I'm trying to do it via UserService's AddUser method:

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

The AddSettings method:

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);
    }
}

UserService constructor:

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

And both services are registered in the Startup.cs

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

I believe the issue is on how I inject SettingsService into the UserService, but I couldn't find any relevant information about that.

CodePudding user response:

You should register ISettingsService service:

services.AddTransient<ISettingsService, SettingsService>();

In your example you should register a class that implement ISettingsService interface like UserRepository class that implement IUserRepository interface

  • Related