我需要在ASP.NET MVC 5应用程序中使用Ninject将RoleManager注入Controller。我在DI和Ninject中都是全新的,所以我不完全理解Ninject的所作所为。我使用Ninject 3.3.4,来自Identity 2.0和EF6.2的标准RoleManager。我的绑定如下:
public class NinjectRegistrations : NinjectModule
{
public override void Load()
{
Bind<HeroesContext>().ToSelf();
Bind<IRepository<Hero>>().To<HeroRepository>();
Bind<IRepository<Ability>>().To<AbilityRepository>();
Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>().WithConstructorArgument("context", new HeroesContext());
Bind<UserManager<ApplicationUser>>().ToSelf();
Bind<HttpContextBase>().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InTransientScope();
Bind<ApplicationSignInManager>().ToMethod(context =>
{
var cbase = new HttpContextWrapper(HttpContext.Current);
return cbase.GetOwinContext().Get<ApplicationSignInManager>();
});
Bind<ApplicationUserManager>().ToSelf();
Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>();
Bind<RoleManager<IdentityRole, string>>().ToSelf();
}
}
在RoleManager之前我已经在HomeController中成功注入了两个存储库,他们的工作正常。我还在AdminController和AccountController中注入了ApplicationUserManager,在AccountController中注入了ApplicationSignInManager,看起来他们的工作也很好,因为我可以登录。当前的问题与RoleManager有关,起初什么都没有用。经过一些谷歌搜索后,我发现this question,部分帮助。现在,当我尝试使用AdminController获取用户列表时,我得到了这个和基本的建议:
Ninject.ActivationException:激活DbConnection时出错
没有匹配的绑定可用,并且该类型不可自我绑定。
激活路径:
5)将依赖关系DbConnection注入到DbContext类型的构造函数的existingConnection中
4)将依赖关系DbContext注入RoleStore类型的构造函数的参数上下文中{IdentityRole,string,IdentityUserRole}
3)将依赖IRoleStore {IdentityRole,string}注入RoleManager类型的构造函数的参数存储中{IdentityRole,string}
2)将依赖RoleManager {IdentityRole,string}注入到AdminController类型的构造函数的参数roleManager中
1)请求AdminController
我试图找到解决方案,但没有发现任何有用的东西。您可以在下面找到AdminController的构造函数,Application_Start()和上下文的代码(我不确定是否需要它)。请帮助,我的招聘取决于此。
public class AdminController : Controller
{
private readonly ApplicationUserManager _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public AdminController(ApplicationUserManager userManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
}
protected void Application_Start()
{
Database.SetInitializer<HeroesContext>(new DbInitializer());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var registrations = new NinjectRegistrations();
var kernel = new StandardKernel(registrations);
kernel.Unbind<ModelValidatorProvider>();
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
public class HeroesContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Hero> Heroes { get; set; }
public DbSet<Ability> Abilities { get; set; }
public HeroesContext() : base("DefaultConnection", throwIfV1Schema: false)
{
}
public static HeroesContext Create()
{
return new HeroesContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Hero>().HasMany(n => n.Abilities)
.WithRequired(n => n.Hero)
.HasForeignKey(n => n.HeroId);
}
}
答案 0 :(得分:1)
我猜你的RoleStore
类期望类型DbContext
的依赖。由于你没有对DbContext
的任何约束,Ninject会回归隐含的自我绑定。这意味着它尝试通过构造函数创建DbContext
:
public DbContext(DbConnection existingConnection, bool contextOwnsConnection)
但在此之前,它无法按照消息中的说明创建DbConnection
。
解决方案是:
更改绑定:Bind<DbContext>().To<HeroesContext>();
或将RoleStore
中的相关类型更改为HeroesContext