简体   繁体   中英

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM