我已下载此示例,我可以在其中尝试ASP.NET MVC 5中的Identity Provider功能:
http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples
我已经多次使用Unity DI来获取我的存储库,服务,工作单元和其他没有问题的东西。
但是现在,当我安装Unity DI时,我在使用身份提供商的接口和DI方面遇到了很多问题。
我刚下载了示例的代码并且我已经配置了Unity DI,但是我不想将Unity用于身份会员资格,我想使用Unity DI只是为了我的东西(IRepository,IService,IUnitOfWork)等等。)
我尝试注册用户时出现此错误:
目前的类型, Microsoft.AspNet.Identity.IUserStore`1 [Ecoavantis.Interactive.GCI.Models.ApplicationUser] 是一个接口,无法构造。你错过了一个类型吗? 映射?
我读了一些帖子,其中他们说我必须包含这样的东西,但我不需要在身份提供者中注入依赖...
container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
有人可以帮我吗?
代码示例:
/// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
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<DbContext, ApplicationDbContext>(new ContainerControlledLifetimeManager());
container.RegisterType<IUnitOfWorkAsync, UnitOfWork>(new PerRequestLifetimeManager());
container.RegisterType<IMainPanelService, MainPanelService>();
container.RegisterType(typeof (IRepositoryAsync<>), typeof (Repository<>));
container.RegisterType<IDataContextAsync, ecoavantisinteractivegciContext>(new PerRequestLifetimeManager());
}
答案 0 :(得分:47)
好的,我已经解决了我的问题,我已经用这种方法注入了依赖项,但我不太清楚为什么它的工作......
现在,Identity与我的Unity DI一起正常工作
container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<RolesAdminController>(new InjectionConstructor());
container.RegisterType<ManageController>(new InjectionConstructor());
container.RegisterType<UsersAdminController>(new InjectionConstructor());
答案 1 :(得分:13)
在我的情况下,我在RegisterComponents方法中将以下内容添加到UnityConfig.cs中。
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
答案 2 :(得分:9)
谢谢化辰!由于我没有足够的回复评论,我发布了一个新的答案。我假设chemitaxis自己创建了最后3个控制器,因为只是注册第一个AccountController
似乎对我有用,即使我扩展了User
模型属性:
container.RegisterType<AccountController>(new InjectionConstructor());
答案 3 :(得分:0)
Update for chemitaxis and hvaughan3 answer. Since you say it happens when you try to register a user it is probably in the standard AccountController
. The original problem for this is that Unity tries to call the constructor with two parameters, for example:
public AccountController(
ApplicationUserManager userManager,
ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
The following line will tell Unity to call the parameterless constructor instead and won't need anything else.
container.RegisterType<AccountController>(new InjectionConstructor());
I hope this explanation helps.