尝试将RoleManager注入ASP.NET Core 2.2控制器时出现错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试将RoleManager注入ASP.NET Core 2.2控制器时出现错误相关的知识,希望对你有一定的参考价值。
我正在尝试将RoleManager
注入我的控制器,但出现此错误:
InvalidOperationException:在尝试激活'StoreProject.Controllers.ProductController'时,无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`1 [StoreProject.Models.User]'的服务。
我的Startup
代码:
public void ConfigureServices(IServiceCollection services)
services.AddMvc();
services.AddMemoryCache();
services.AddSession();
services.AddTransient<IStoreRepository, StoreRepository>();
services.AddIdentity<User,IdentityRole<long>>(options =>
options.User.RequireUniqueEmail = true;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
).AddEntityFrameworkStores<StoreContext>();
services.AddDbContext<StoreContext>(options =>
options.UseSqlServer("Server=LAPTOP-14M18EPM\\RONSQL;Database=projectDB;Trusted_Connection=True"));
我的控制器:
public class AccountController : Controller
private readonly SignInManager<User> _signInManager;
private readonly UserManager<User> _userManager;
private readonly RoleManager<User> _roleManager;
IStoreRepository _repository;
private readonly IHostingEnvironment _env;
public AccountController(SignInManager<User> signInManager,
UserManager<User> userManager,
RoleManager<User> roleManager,
IStoreRepository storeRepository,
IHostingEnvironment env)
_signInManager = signInManager;
_userManager = userManager;
_roleManager = roleManager;
_repository = storeRepository;
_env = env;
问题出在哪里?
答案
您的问题很可能是这个:
RoleManager<User> roleManager
以上是关于尝试将RoleManager注入ASP.NET Core 2.2控制器时出现错误的主要内容,如果未能解决你的问题,请参考以下文章
Asp.Net core MVC6如何在Identity 3中初始添加角色