在StructureMap的IoC中包含ASP.NET标识

问题描述:

我正在尝试在使用Structuremap 3.0进行学习的项目中添加IoC。在StructureMap的IoC中包含ASP.NET标识

我按照这个指南here它使用Unity,但我试图在结构图中实现同样的事情。

My codes are here

我的注册表

public class IdentityRegistry : Registry 
{ 
    public IdentityRegistry() 
    { 
     Scan(scan => 
     { 
      scan.TheCallingAssembly(); 
      scan.WithDefaultConventions(); 
     }); 

     For<ApplicationUserManager>().Use<ApplicationUserManager>(() => new ApplicationUserManager(new UserStore<ApplicationUser>())); 

     For<ApplicationSignInManager>().Use<ApplicationSignInManager>(() => new ApplicationSignInManager(new ApplicationUserManager(new UserStore<ApplicationUser>()), HttpContext.Current.GetOwinContext().Authentication)); 

     For<ApplicationDbContext>().Use<ApplicationDbContext>(() => new ApplicationDbContext()); 

     For<IAuthenticationManager>().Use(c => HttpContext.Current.GetOwinContext().Authentication); 

     For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser>>().Ctor<ApplicationDbContext>(); 
    } 
} 

它给了我一个错误,“实体类型ApplicationUser是不是该机型为当前上下文的一部分。”我不知道我是否在结构图中正确写入

我没有大量使用StructureMap,但是您的注册对我来说看起来不对。你注册代表而不是让容器来处理这个问题。而不是使用的代表做了登记类型:

For<ApplicationUserManager>().TheDefaultIsConcreteType<ApplicationUserManager>(); 

和变更登记的所有其余是这个样子(见documentation更多解释)。

而且很有可能您的错误不会消失,只有当您尝试使用ApplicationDbContext时才会消失。在这种情况下,我们需要查看这个类的代码 - 数据库连线的方式有问题。

+0

如果我尝试这样做,MVC路由不响应正确的构造函数,它给了我一个错误“没有为此对象定义的无参数构造函数”。 – Iojecht 2014-11-25 04:04:37

+0

好像你缺少'DependencyResolver.SetResolver(新的StructureMapDependencyResolver(容器));'刚刚配置完你的StructureMap容器​​。 – trailmax 2014-11-25 09:14:53

+0

我已经在指南中使用WebActivatorEx事件在我的依赖项中添加了相同内容。无论如何,我已经添加了我的完整源代码github – Iojecht 2014-11-25 09:38:08