【发布时间】:2016-02-17 15:31:41
【问题描述】:
如果我浏览 http://localhost:58472/Account/Register 我有这个例外
System.InvalidOperationException:当前类型IUserStore<ApplicationUser>是一个接口,不能构造。您是否缺少类型映射?
这是我的代码:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{ }
public static ApplicationUserManager Create(
IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(
new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())
);
// other code
return manager;
}
}
这是控制器的代码:
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
public AccountController()
{
}
public AccountController(
ApplicationUserManager userManager,
ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ??
HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ??
HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
[HttpGet]
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
}
所以你可以看看你是否阅读了我的代码,当你创建一个新的 MVC 项目时,我对原始代码没有任何改变,但是我遇到了上面的错误。我做错了什么?
我已经很好地创建了一个名为 ProfileController 的新控制器,其代码与我的第二个代码块中的代码相同,但没有 ApplicationSignInManager,因为我不需要它。
我还使用 Unity 容器来注册和管理服务。代码如下:
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<IGenericRepo<Category>, GenericRepo<Category>>();
container.RegisterType<ICategoryService, CategoryService>();
//container.RegisterType<IUser, ApplicationUser>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
如果我在上面的代码中取消注释,运行网站时没有任何区别。
更新:
根据@Jay 的评论,我已在 Unity 管理器中添加了此代码。
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
现在我遇到了这个异常:
System.InvalidOperationException: 当前类型System.Data.Common.DbConnection是一个抽象类,无法构造。您是否缺少类型映射?
以下是堆栈跟踪中的一些信息:
-
InvalidOperationException尝试创建
AccountController类型的控制器时出错。确保控制器有一个无参数的公共构造函数。是的,我有一个无参数的公共构造函数。
-
ResolutionFailedException解析依赖失败,类型 =
"AccountController",名称 ="(none)"。
所以我在 Unity 管理器中添加了这一行:
container.RegisterType<IDbConnection, DbConnection>();
但我仍然有同样的例外。
当前类型,
System.Data.Common.DbConnection...
更新:
通过@RandyLevy 的评论我已经尝试过这个,通过这个问题来帮助Configure Unity DI for ASP.NET Identity:
-
将此添加到
AccountController:// two fields added private readonly UserManager<ApplicationUser> _userManager; private readonly RoleManager<IdentityRole> _roleManager; // constructor added public AccountController( IUserStore<ApplicationUser> userStore, IRoleStore<IdentityRole> roleStore, ApplicationSignInManager signInManager) { _userManager = new UserManager<ApplicationUser>(userStore); _roleManager = new RoleManager<IdentityRole>(roleStore); SignInManager = signInManager; // I need this } public ApplicationSignInManager SignInManager // I need this { get { return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>(); } private set { _signInManager = value; } } public UserManager<ApplicationUser> UserManager // replace by other property // type of ApplicationUserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } //private set // comment this because _usermanager is readonly. //{ // _userManager = value; //} } //public ApplicationUserManager UserManager // replaced by the property type of // // UserManager<ApplicationUser> //{ // get // { // return _userManager ?? // HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); // } // private set // { // _userManager = value; // } //} -
将此添加到 UnityManager 中:
InjectionConstructor accountInjectionConstructor = new InjectionConstructor(new ApplicationDbContext()); container.RegisterType<AccountController>(); container.RegisterType<IController, Controller>(); container.RegisterType <IRoleStore<IdentityRole>, RoleStore<IdentityRole>> (accountInjectionConstructor); // on this line container.RegisterType <IUserStore<ApplicationUser>, UserStore<ApplicationUser>> (accountInjectionConstructor);
但我在注释行出现了这个错误(没有运行时异常):
类型
RoleStore<IdentityRole>不能在下面的泛型类型或方法中用作类型参数TTo:UnityContainerExtensions.RegisterType<TFrom, TTo> (IUnityContainer, params InjectionMember[])没有从
RoleStore<IdentityRole>到IRoleStore<IdentityRole>的隐式引用转换。
【问题讨论】:
-
您是否缺少 IUserStore
的寄存器类型? -
@Jay:谢谢你的提示,但我现在有另一个例外。我的问题中有更多信息。
-
看this问题。
标签: c# asp.net-mvc unity-container invalidoperationexception user-management