为ASP.NET身份配置Unity DI

Ben*_* E. 18 c# asp.net dependency-injection unity-container asp.net-identity

我使用Unity成功为所有常规构造注射,如仓库等,但我不能让它与ASP.NET身份类的工作.设置是这样的:

public class AccountController : ApiController
{
    private UserManager<ApplicationUser> _userManager { get; set; }

    public AccountController(UserManager<ApplicationUser> userManager)
    {
        if (userManager == null) { throw new ArgumentNullException("userManager"); }
        _userManager = userManager;
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

使用Unity的这些配置:

unity.RegisterType<AccountController>();
unity.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
unity.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
Run Code Online (Sandbox Code Playgroud)

这与Autofac,Ninject的其他帖子相同,例如,但在我的情况下它不起作用.错误消息是:

尝试创建"AccountController"类型的控制器时发生错误.确保控制器具有无参数的公共构造函数.手动创作当然是:

public AccountController()
    : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>("Mongo")))
{
}
Run Code Online (Sandbox Code Playgroud)

怎么了?

UPDATE

正如@emodendroket所建议的,缩短代码就可以了.不需要Unity.Mvc包.

unity.RegisterType<IUserStore<IdentityUser>, 
  MyCustomUserStore>(new HierarchicalLifetimeManager());
Run Code Online (Sandbox Code Playgroud)

public AccountController(UserManager<IdentityUser> _userManager,
  IAccountRepository _account)
{
    // ...
Run Code Online (Sandbox Code Playgroud)

Hor*_*Net 21

您还需要解析UserManager.以下是如何使用UserManagerRoleManager执行此操作的示例.在这个示例中,我使用常规的Unity 3软件包而不是其中一个派生或引导程序(过去有一些问题).

的AccountController

private readonly UserManager<ApplicationUser> _userManager;

private readonly RoleManager<IdentityRole> _roleManager;

public AccountController(IUserStore<ApplicationUser> userStore, IRoleStore<IdentityRole> roleStore)
{
  _userManager = new UserManager<ApplicationUser>(userStore);
  _roleManager = new RoleManager<IdentityRole>(roleStore);
}
Run Code Online (Sandbox Code Playgroud)

Unity Bootstrapper

var accountInjectionConstructor = new InjectionConstructor(new IdentitySampleDbModelContext(configurationStore));
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(accountInjectionConstructor);
container.RegisterType<IRoleStore<IdentityRole>, RoleStore<IdentityRole>>(accountInjectionConstructor);
Run Code Online (Sandbox Code Playgroud)

  • @MikeDymond我认为它应该是container.RegisterType <IRoleStore <IdentityRole,string>,RoleStore <IdentityRole >>(accountInjectionConstructor).字符串部分丢失了 (5认同)
  • 我使用相同的代码,UserStore注册正常,但它不会编译RoleStore注册.错误=类型'Microsoft.AspNet.Identity.EntityFramework.RoleStore <Microsoft.AspNet.Identity.EntityFramework.IdentityRole>'不能在泛型类型或方法'Microsoft.Practices.Unity.UnityContainerExtensions中用作类型参数'TTo'. RegisterType <TFrom,TTo>(Microsoft.Practices.Unity.IUnityContainer,params Microsoft.Practices.Unity.InjectionMember [])'. (3认同)

Kam*_*ran 14

正如来自enterpriseframework.com的这篇博文所说:

首先,为ASP.NET MVC Nuget包添加Unity Bootstrapper.

  1. 在Visual Studio"解决方案资源管理器"中>右键单击Web项目的"引用"节点>单击"管理NuGet包".

  2. 从左侧菜单中选择Online> All

  3. 在右上角的搜索框>在线搜索(Ctrl + E)>键入"ASP.NET MVC的Unity bootstrapper".
  4. 选择"ASP.NET MVC的Unity Bootstrapper"并选择Install.
  5. 安装完成后单击"关闭"

然后修改your-Web-project/App_Start/UnityConfig.cs文件并更新using语句,如下所示:

    using System;
    using System.Data.Entity;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;
    using MicrosoftIdentity.Controllers;
    using MicrosoftIdentity.Models;
Run Code Online (Sandbox Code Playgroud)

最后,在同一个文件中,更新RegisterTypes方法如下:

        public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            // container.LoadConfiguration();

            // TODO: Register your types here
            container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
            container.RegisterType<UserManager<ApplicationUser>>();
            container.RegisterType<DbContext, ApplicationDbContext>();
            container.RegisterType<ApplicationUserManager>();
            container.RegisterType<AccountController>(new InjectionConstructor());
        }
Run Code Online (Sandbox Code Playgroud)

HTH