我如何注入身份班,Ninject? [英] How do I inject Identity classes with Ninject?
问题描述
我想在一个类中使用的UserManager,但我得到这个错误:
错误激活IUserStore {} ApplicationUser
没有匹配的绑定是可用的,并且类型不是自我绑定。
我使用的是默认的Startup.cs,其中每个请求设置一个实例:
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext< ApplicationUserManager>(ApplicationUserManager.Create);
我能够得到ApplicationDbContext实例,我相信通过Owin是越来越注射(是真的吗?)
公共类GenericRepository< T> :IGenericRepository< T>其中T:类
{
私人ApplicationDbContext语境; 公共GenericRepository(ApplicationDbContext上下文)
{
this.context =背景;
}
}
但我不能做的UserManager相同的(它抛出之前所示的错误):
公共类AnunciosService:IAnunciosService
{
私人IGenericRepository< Anuncio> _repo;
私人ApplicationUserManager _userManager; 公共AnunciosService(IRepositorioGenerico< Anuncio>回购,ApplicationUserManager的UserManager)
{
_repo =回购;
_userManager =的UserManager;
}
}
控制器使用的UserManager是这样的:
公共ApplicationUserManager的UserManager
{
得到
{
返回_userManager? 。HttpContext.GetOwinContext()GetUserManager< ApplicationUserManager>();
}
私订
{
_userManager =价值;
}
}
我使用ninject注入我的其他类,但我怎么注入的UserManager与它的依赖和避免使用它一样,在我的控制器?
我注入像这样
<$p$p><$c$c>kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>();kernel.Bind&LT;&的UserManager LT; ApplicationUser&GT;&GT;()ToSelf()。
而现在它的工作,因为它应该,但是,我不知道这是否会导致通过的UserManager的多个实例被引用或不符合实体的问题。
I'm trying to use UserManager in a class, but I'm getting this error:
Error activating IUserStore{ApplicationUser}
No matching bindings are available, and the type is not self-bindable.
I'm using the default Startup.cs, which sets a single instance per request:
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
I'm able to get the ApplicationDbContext instance, which I believe is getting injected by Owin (Is that true?):
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private ApplicationDbContext context;
public GenericRepository(ApplicationDbContext context)
{
this.context = context;
}
}
But I can't do the same with UserManager (It throws the error shown before):
public class AnunciosService : IAnunciosService
{
private IGenericRepository<Anuncio> _repo;
private ApplicationUserManager _userManager;
public AnunciosService(IRepositorioGenerico<Anuncio> repo, ApplicationUserManager userManager)
{
_repo = repo;
_userManager = userManager;
}
}
The controller uses the UserManager like this:
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
I'm using ninject to inject my other classes, but how do I inject the UserManager with it's dependencies and avoid using it like that in my controllers?
I injected It like this
kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>();
kernel.Bind<UserManager<ApplicationUser>>().ToSelf();
And now It's working as it should, however, I'm not sure whether this will cause problems with entities being referenced by multiple instances of UserManager or not.
这篇关于我如何注入身份班,Ninject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!