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?