我在帐户控制器中有以下代码
public sealed class AccountController : Controller
{
#region Fields
private readonly IUserService _userService;
#endregion
#region Ctor
public AccountController(IUserService userService)
{
_userService = userService;
}
#endregion
#region Register
public IActionResult Register()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Register(RegisterModel model)
{
if (!ModelState.IsValid)
{
return View();
}
var user = _userService.Register(model);
var result = await _userService.CreateAsync(user, model.Password);
if (result)
{
await _userService.SignInAsync(user, false);
return RedirectToAction("Index", "Home");
}
return View(model);
}
我的startup.cs看起来像下面的代码
services.AddDbContext<CourseAppIdentityContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CourseAppIdentityContext"), sqlServerOptions => sqlServerOptions.MigrationsAssembly("Tada.CourseApp.Data")));
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<CourseAppIdentityContext>()
.AddDefaultTokenProviders();
我的UserService看起来像
public class UserService : IUserService
{
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
public UserService(UserManager<IdentityUser> userManager,
SignInManager<IdentityUser> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
public async Task<bool> CreateAsync(IdentityUser user, string password)
{
var result = await _userManager.CreateAsync(user, password);
if (result.Succeeded)
{
return true;
}
return false;
}
当我运行应用程序并打开注册链接
发生此错误
InvalidOperationException:尝试激活“ Tada.CourseApp.Business.Implementations.UserService”时,无法解析类型为“ Microsoft.AspNetCore.Identity.UserManager`1 [Microsoft.AspNet.Identity.EntityFramework.IdentityUser]”的服务。 / p>
我有这样的DI
services.AddSingleton<IUserService, UserService>();
我该如何解决?
谢谢!