【发布时间】:2018-07-07 16:15:41
【问题描述】:
我需要在 ASP.NET MVC 5 应用程序中使用 Ninject 将 RoleManager 注入控制器。我对 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 中注入了 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);
}
}
【问题讨论】:
-
“我是 DI 的新手”。你绝对应该从this introduction to DI开始。
-
@Steven 感谢这本书。我战斗我需要这样的东西来开始理解 DI。虽然大的英文资料对我来说有点难。
-
请注意,这个免费章节只有大约 30 页。如果你问我,这很可行。这将是您度过的美好时光。而且,如果你不读书,你怎么能学到东西?
-
@Steven 我的意思是我通常很难理解一些重要的事情和细节。我阅读文章比阅读书籍更多。而且书里有很多东西我永远不会用,但会花时间去理解它们。
标签: asp.net asp.net-mvc dependency-injection inversion-of-control ninject