C# 当前类型,IUserStore<;应用程序用户>;和DbConnection分别是接口和抽象类,无法构造

C# 当前类型,IUserStore<;应用程序用户>;和DbConnection分别是接口和抽象类,无法构造,c#,asp.net-mvc,unity-container,invalidoperationexception,user-management,C#,Asp.net Mvc,Unity Container,Invalidoperationexception,User Management,如果我浏览到http://localhost:58472/Account/Register我有这个例外 系统。无效操作异常:当前类型,IUserStore: 将其添加到帐户控制器中: // two fields added private readonly UserManager<ApplicationUser> _userManager; private readonly RoleManager<IdentityRole> _roleManager; // const

如果我浏览到
http://localhost:58472/Account/Register
我有这个例外

系统。无效操作异常:
当前类型,
IUserStore:

  • 将其添加到
    帐户控制器中

    // 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;
    //    }
    //}
    
    没有从
    RoleStore
    IRoleStore
    的隐式引用转换


    我终于找到了解决问题的办法。我已将此代码添加到Unity manager中

    container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
    
    container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
    container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
    container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
    container.RegisterType<AccountController>(new InjectionConstructor());
    
    container.RegisterType(新的层次结构CallifetimeManager());
    RegisterType(新的层次结构CallifetimeManager());
    RegisterType(新的层次结构CallifetimeManager());
    RegisterType(新的InjectionConstructor());
    
    更新H.Pauwelyn的答案。最初的问题是Unity试图用两个参数调用构造函数

    public AccountController(
        ApplicationUserManager userManager,
        ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }
    
    下一行将告诉Unity调用无参数构造函数,而不需要其他任何东西。这对我有用

    container.RegisterType<AccountController>(new InjectionConstructor());
    
    container.RegisterType(新注入构造函数());
    
    您是否缺少IUserStore的寄存器类型?也许会有帮助。@Jay:谢谢你的提示,但我现在有一个例外。我的问题中有更多信息。看问题。我只是在安装Unity后才开始得到OP所得到的错误。似乎与Unity的依赖解析程序不使用本机标识功能有关。注册这些类型修复了它。
    // 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;
    //    }
    //}
    
    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);
    
    UnityContainerExtensions.RegisterType<TFrom, TTo>
        (IUnityContainer, params InjectionMember[])
    
    container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
    container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
    container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
    container.RegisterType<AccountController>(new InjectionConstructor());
    
    public AccountController(
        ApplicationUserManager userManager,
        ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }
    
    container.RegisterType<AccountController>(new InjectionConstructor());