che*_*xis 30 c# asp.net-mvc dependency-injection asp.net-identity
我已下载此示例,我可以在其中尝试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]是一个接口,无法构造.你错过了类型映射吗?
我读了一些帖子,其中他们说我必须包含这样的内容,但我不需要在Identity Provider中注入依赖项...
container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
代码示例:
/// <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());
}
Run Code Online (Sandbox Code Playgroud)
che*_*xis 48
好吧,我已经解决了我的问题,我已经用这个方法注入了依赖项,但我不太清楚为什么它的工作......
现在,Identity可与我的Unity DI一起使用
container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<RolesAdminController>(new InjectionConstructor());
container.RegisterType<ManageController>(new InjectionConstructor());
container.RegisterType<UsersAdminController>(new InjectionConstructor());
Run Code Online (Sandbox Code Playgroud)
Ola*_*emo 13
在我的例子中,我将以下内容添加到RegisterComponents方法中的UnityConfig.cs中.
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
Run Code Online (Sandbox Code Playgroud)
hva*_*an3 10
谢谢铁硅脂!由于我没有足够的回复评论,我发布了一个新的答案.我假设chemitaxis自己创建了最后3个控制器,因为只是注册第一个AccountController似乎对我有用,即使我扩展了User模型属性:
container.RegisterType<AccountController>(new InjectionConstructor());
Run Code Online (Sandbox Code Playgroud)
更新 chemitaxis 和 hvaughan3 答案。既然你说当你尝试注册用户时就会发生这种情况,它可能是在标准中的AccountController。最初的问题是 Unity 尝试使用两个参数调用构造函数,例如:
public AccountController(
ApplicationUserManager userManager,
ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
Run Code Online (Sandbox Code Playgroud)
以下行将告诉 Unity 改为调用无参数构造函数,并且不需要任何其他内容。
container.RegisterType<AccountController>(new InjectionConstructor());
Run Code Online (Sandbox Code Playgroud)
我希望这个解释有帮助。
| 归档时间: |
|
| 查看次数: |
13602 次 |
| 最近记录: |