System.AggregateException exception while attempting to register a service in ASP.NET CORE 3.1

I have made an ISignUp interface which is implemented by the SignUp class to create a new user. I am using Dependancy Injection (DI). The issue is that I cannot register this service. Instead of creating new users in the SignUpController directly, I rather prefer calling a helper class via the Interface it implements.

I am sure I am missing something here. Let me post the code and the exception so that someone may point out what I am missing.

Project Details:

ASP.NET CORE 3.1

I will appreciate help in pointing out the issue here.

Below is the ISignUp interface:

public interface ISignUp
{
    public Task<bool> IsUserCreatedAsync(ApplicationUser user, string password);
}

Below is the SignUp class implementing the ISignUp Interface:

public class SignUp : ISignUp
{
    private readonly UserManager<ApplicationUser> userManager;

    public SignUp(UserManager<ApplicationUser> _userManager)
    {
        userManager = _userManager;
    }

    public async Task<bool> IsUserCreatedAsync(ApplicationUser user, string password)
    {
        // create a new user account
        var result = await userManager.CreateAsync(user, password);

        // check result
        if (result.Succeeded)
        {
            return true;
        }
        return false;
    }
}

Below is the SignUpController:

public class SignUpController : Controller
{
    private readonly ISignUp signUp;

    public SignUpController(ISignUp _signUp)
    {
        signUp = _signUp;
    }
}

Below checking if the user is created:

  // create new user
  var userCreated = await signUp.IsUserCreatedAsync(user, model.Password);

  // check result
  if (userCreated)
  {
     // user created, log them in
  }

Registering the service in the StartUp.cs Class:

  // registering an ISignUp service and its implementation
  services.AddScoped<ISignUp, SignUp>();

When I build and run, I get this exception:

System.AggregateException: ‘Some services are not able to be constructed (Error while validating the service descriptor ‘ServiceType: School_ERP_Software.Helpers.Interfaces.ISignUp Lifetime: Scoped ImplementationType: School_ERP_Software.Helpers.Implementation.SignUp’: Unable to resolve service for type ‘Microsoft.AspNetCore.Identity.UserManager`1[School_ERP_Software.Database.Model.ApplicationUser]’ while attempting to activate ‘School_ERP_Software.Helpers.Implementation.SignUp’.)’

What am I missing here?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You only have on constructor here:

public class SignUp : ISignUp
{
    private readonly UserManager<ApplicationUser> userManager;
    public SignUp(UserManager<ApplicationUser> _userManager)
    {
        userManager = _userManager;
    }

The framework, tries to initialize the SignUp object with the only constructor it can find, which is the one above.

It does not know what value to provide, as you have not resolved this dependency for the DI container in your configuration, so you get the error above, hence the error

Unable to resolve service for type ‘Microsoft.AspNetCore.Identity.UserManager1

You need to register the UserManager service to get rid of the exception.

Check ASP.NET Core Identity does not inject UserManager<ApplicationUser>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x