我成功地将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;
}
// ...
}
使用Unity的这些配置:
unity.RegisterType<AccountController>();
unity.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
unity.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
这和Autofac的其他帖子是一样的,例如,Ninject,但在我的例子中不起作用。错误消息为:
尝试创建'AccountController‘类型的控制器时出错。确保控制器有一个无参数的公共构造函数。当然,手动创建工作:
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>("Mongo")))
{
}
怎么了?
更新
正如@emodendroket所建议的那样,将代码缩短到这一点是可行的。不需要Unity.Mvc包。
unity.RegisterType<IUserStore<IdentityUser>,
MyCustomUserStore>(new HierarchicalLifetimeManager());
和
public AccountController(UserManager<IdentityUser> _userManager,
IAccountRepository _account)
{
// ...
发布于 2014-02-21 17:39:20
您还需要解析UserManager。以下是如何使用UserManager和RoleManager执行此操作的示例。在这个示例中,我使用的是普通的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);
}
统一引导程序
var accountInjectionConstructor = new InjectionConstructor(new IdentitySampleDbModelContext(configurationStore));
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(accountInjectionConstructor);
container.RegisterType<IRoleStore<IdentityRole>, RoleStore<IdentityRole>>(accountInjectionConstructor);
发布于 2015-02-01 20:27:48
正如这个blog post from enterpriseframework.com所说的:
首先,添加Unity Bootstrapper for ASP.NET MVC Nuget包。
在您的Visual Studio“解决方案资源管理器”中,右键单击您的Web项目的"References“节点>单击"Manage
后选择“Unity Close
然后修改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<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
container.RegisterType<UserManager<ApplicationUser>>();
container.RegisterType<DbContext, ApplicationDbContext>();
container.RegisterType<ApplicationUserManager>();
container.RegisterType<AccountController>(new InjectionConstructor());
}
HTH
发布于 2014-02-21 15:56:30
Install-Package Unity.AspNet.WebApi
您需要在HttpConfiguration.DependencyResolver
属性下注册Unity。这使得WebApi
知道它需要使用Unity而不是反射来实例化你的控制器。
最简单的方法是使用上面的nuget包。
https://stackoverflow.com/questions/21927785
复制相似问题