【问题标题】:The current types, IUserStore<ApplicationUser> and DbConnection, are respectively an interface and abstract class and cannot be constructed当前类型 IUserStore<ApplicationUser> 和 DbConnection 分别是接口和抽象类,无法构造
【发布时间】:2016-02-17 15:31:41
【问题描述】:

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

System.InvalidOperationException: 当前类型IUserStore&lt;ApplicationUser&gt;是一个接口,不能构造。您是否缺少类型映射?

这是我的代码:

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是一个抽象类,无法构造。您是否缺少类型映射?

以下是堆栈跟踪中的一些信息:

  1. InvalidOperationException

    尝试创建AccountController 类型的控制器时出错。确保控制器有一个无参数的公共构造函数。

    是的,我有一个无参数的公共构造函数。

  2. ResolutionFailedException

    解析依赖失败,类型 = "AccountController",名称 = "(none)"

所以我在 Unity 管理器中添加了这一行:

container.RegisterType<IDbConnection, DbConnection>();

但我仍然有同样的例外。

当前类型,System.Data.Common.DbConnection...


更新:

通过@RandyLevy 的评论我已经尝试过这个,通过这个问题来帮助Configure Unity DI for ASP.NET Identity

  1. 将此添加到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;
    //    }
    //}
    
  2. 将此添加到 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&lt;IdentityRole&gt;不能在下面的泛型类型或方法中用作类型参数TTo

UnityContainerExtensions.RegisterType<TFrom, TTo>
    (IUnityContainer, params InjectionMember[])

没有从RoleStore&lt;IdentityRole&gt;IRoleStore&lt;IdentityRole&gt; 的隐式引用转换。

【问题讨论】:

  • 您是否缺少 IUserStore 的寄存器类型?
  • stackoverflow.com/questions/21927785/… 可能会有所帮助。
  • @Jay:谢谢你的提示,但我现在有另一个例外。我的问题中有更多信息。
  • this问题。

标签: c# asp.net-mvc unity-container invalidoperationexception user-management


【解决方案1】:

我终于找到了解决问题的方法。我已将此代码添加到 Unity 管理器中。

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());

【讨论】:

  • 我在安装 Unity 后才开始收到 OP 收到的错误。似乎与 Unity 的依赖解析器不使用本机身份功能有关。注册这些类型可以修复它。
【解决方案2】:

更新 H. Pauwelyn 的答案。最初的问题是 Unity 尝试使用两个参数调用构造函数。

public AccountController(
    ApplicationUserManager userManager,
    ApplicationSignInManager signInManager)
{
    UserManager = userManager;
    SignInManager = signInManager;
}

以下行将告诉 Unity 改为调用无参数构造函数,并且不需要任何其他内容。这对我有用。

container.RegisterType<AccountController>(new InjectionConstructor());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多