我在web应用程序中使用了ninject.web扩展,但现在在解决global.asax的Session_Start方法中的依赖项时遇到了问题
这是我的global.asax
public class Global : NinjectHttpApplication
{
[Inject]
IUserManagement um { get; set; }
protected void Session_Start(object sender, EventArgs e)
{
if (WebUser.user != null)
{
if (HttpContext.Current.Session[ChiaveSessioneUtente] == null)
{
if (HttpContext.Current != null)
HttpContext.Current.Session.Add(ChiaveSessioneUtente, um.ResolveRequestingUser(Request));
}
}
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
return kernel;
}
private void RegisterServices(IKernel kernel)
{
var modules = new List<INinjectModule>
{
new BusinessLogicModule(),
};
kernel.Load(modules);
}
}Ninject模块
public class BusinessLogicModule : NinjectModule
{
public override void Load()
{
Bind<IBusinessInquiry>().To<BusinessInquiry>();
Bind<IUserManagement>().To<UserManagement>();
}
}但是,当我启动应用程序时,um为null,即使通过调试我看到CreateKernel在Session_Start之前执行
我还使用NinjectWebCommon尝试了nuget包版本的ninject.web,但结果是相同的:属性没有注入,并且为空
相反,在我的webForm中,注入的所有属性都可以正常工作,所以问题只出现在global.asax或session_Start方法上
发布于 2016-09-07 18:21:04
属性Setter注入
下面是使用属性Setter注入的同一个Samurai类的示例。对于要注入的属性,必须使用Inject对其进行注释。
class Samurai
{
[Inject]
public IWeapon Weapon { private get; set; }
public void Attack(string target)
{
this.Weapon.Hit(target);
}
}发布于 2016-09-07 18:02:05
尝试删除Global.asax中的Inject属性
https://stackoverflow.com/questions/39365820
复制相似问题