Reputation: 119
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;}
}
}
Upvotes: 0
Views: 2202
Reputation: 5118
This line:
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
I believe should have ApplicationUser
instead of IdentityUser
as the generic parameter for AddDefaultIdentity
.
Upvotes: 1