0

Trying to use Postman to POST some data to ms sql server and test my api. I'm getting error:

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyOrg.Models.User]' while attempting to activate 'MyOrg.Controllers.AccountsController'.

I have checked and tried couple of solutions of similar problems and nothing helped.

Here is my code:

Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });

        services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
        b => b.MigrationsAssembly("MyOrg")));
    }

AccountsController.cs:

public class AccountsController : Controller
{
    private readonly ApplicationDbContext _appDbContext;
    private readonly UserManager<User> _userManager;
    private readonly IMapper _mapper;

    public AccountsController(UserManager<User> userManager, IMapper mapper, ApplicationDbContext appDbContext)
    {
        _userManager = userManager;
        _mapper = mapper;
        _appDbContext = appDbContext;
    }

    // POST api/accounts
    [HttpPost]
    public async Task<IActionResult> Post([FromBody]RegistrationViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var userIdentity = _mapper.Map<User>(model);
        var result = await _userManager.CreateAsync(userIdentity, model.Password);

        if (!result.Succeeded) return new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState));

        await _appDbContext.UserRoles.AddAsync(new UserRole { IdentityId = userIdentity.Id, Location = model.Location });
        await _appDbContext.SaveChangesAsync();

        return new OkResult();
    }

}

I'm not sure what is wrong. Have you any ideas how to fix that?

0 Answers0