为ASP.NET身份配置Unity DI

我正在成功地使用Unity进行所有常规构造函数注入,例如存储库等,但我无法使用ASP.NET Identity类。 设置是这样的:

public class AccountController : ApiController { private UserManager _userManager { get; set; } public AccountController(UserManager userManager) { if (userManager == null) { throw new ArgumentNullException("userManager"); } _userManager = userManager; } // ... } 

使用Unity的这些配置:

 unity.RegisterType(); unity.RegisterType<UserManager>(new HierarchicalLifetimeManager()); unity.RegisterType<IUserStore, UserStore>(new HierarchicalLifetimeManager()); 

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

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

 public AccountController() : this(new UserManager(new UserStore("Mongo"))) { } 

怎么了?

UPDATE

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

 unity.RegisterType<IUserStore, MyCustomUserStore>(new HierarchicalLifetimeManager()); 

 public AccountController(UserManager _userManager, IAccountRepository _account) { // ... 

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

的AccountController

 private readonly UserManager _userManager; private readonly RoleManager _roleManager; public AccountController(IUserStore userStore, IRoleStore roleStore) { _userManager = new UserManager(userStore); _roleManager = new RoleManager(roleStore); } 

Unity Bootstrapper

 var accountInjectionConstructor = new InjectionConstructor(new IdentitySampleDbModelContext(configurationStore)); container.RegisterType, UserStore>(accountInjectionConstructor); container.RegisterType, RoleStore>(accountInjectionConstructor); 

正如来自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; 

最后,在同一个文件中,更新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, UserStore>(); container.RegisterType>(); container.RegisterType(); container.RegisterType(); container.RegisterType(new InjectionConstructor()); } 

HTH

 Install-Package Unity.AspNet.WebApi 

您需要在HttpConfiguration.DependencyResolver属性下注册Unity。 这允许WebApi知道它需要使用Unity而不是reflection来实现控制器。

最简单的方法是使用上面的nuget包。

AccountController替换此属性

 public ApplicationUserManager UserManager { get { _userManager = new ApplicationUserManager( new UserStore(new ApplicationDbContext())); return _userManager ?? Request.GetOwinContext().GetUserManager(); } private set { _userManager = value; } }