0

I'm wrapping up on tutorial and cannot fix problem in my program. When I go to the my route I get internal server error like this :

An unhandled exception occurred while processing the request. InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[AspNetCoreTodo.Models.ApplicationUser]' while attempting to activate 'AspNetCoreTodo.Controllers.TodoController'. Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[AspNetCoreTodo.Models.ApplicationUser]' while attempting to activate 'AspNetCoreTodo.Controllers.TodoController'.

Judging by this I think the problem lies in my controller, to be specific in my index method. It has worked so far but I got this error when I was trying to find authorized user for todo list. Basically the code is here and I think the error may be in this line :

var items = await _todoItemService.GetIncompleteItemsAsync(currentUser);

Here is my code for controller :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Mvc;
using AspNetCoreTodo.Services;
using AspNetCoreTodo.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;

namespace AspNetCoreTodo.Controllers 
{
    [Authorize]
    public class TodoController : Controller
    {
    private readonly ITodoItemService _todoItemService;
    private readonly UserManager<ApplicationUser> _userManager;
    public TodoController(ITodoItemService todoItemService, UserManager<ApplicationUser> userManager)
    {
        _todoItemService = todoItemService;
        _userManager = userManager;
    }
    public async Task<IActionResult> Index()
    {
        var currentUser = await _userManager.GetUserAsync(User);
        //var   currentUser = await _userManagerFindByIdAsync
        if(currentUser == null)
        {
            return Challenge();
        }
        var items = await _todoItemService.GetIncompleteItemsAsync(currentUser);

        var model = new TodoViewModel()
        {
            Items = items
        };

        return View(model);

    }

Startup.cs //configuration

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlite(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddControllersWithViews();
       services.AddRazorPages();
        services.AddMvc();
        services.AddAuthentication();
       services.AddScoped<ITodoItemService, TodoItemService>();
    }

I solved this but will leave this to anyone who may encounter a similar problem. What I had to do was replacing the IdentityUser with ApplicationUser in my startup.cs file and changing this 2 lines in _LoginPartial.cshtml :

@inject SignInManager IdentityUser SignInManager

@inject UserManager IdentityUser UserManager

with :

@inject SignInManager ApplicationUser SignInManager

@inject UserManager ApplicationUser UserManager

After that you should add type after IdentityDbContex so you would have something like this in your ApplicationDbContext.cs but only if you receive context error :

namespace AspNetCoreTodo.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    public DbSet<TodoItem> Items {get;  set;}
}

}

2
  • 3
    It sounds like you haven't told .Net how to resolve an instance of UserManager<ApplicationUser>. Can you please share your dependency injection configuration? Commented Dec 11, 2019 at 4:49
  • I have added part of the code in startup.cs that might have caused this problem. Commented Dec 11, 2019 at 17:35

1 Answer 1

1

This line:

services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)

I believe should have ApplicationUser instead of IdentityUser as the generic parameter for AddDefaultIdentity.

Sign up to request clarification or add additional context in comments.

4 Comments

When changing to ApplicationUser I get different error when loading page. Error I get here goes like this : InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered. Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType
Maybe take a look at this: stackoverflow.com/a/52568757/12431728
Thank you, this link you've provided with a little bit of tweaks solves the problem.
Awesome, glad to help. Please don't forget to mark the answer as correct if it was most helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.