Ninject 中的 Httpcontext.Session 始终为 null
我使用 ninject 注入 httpcontext,就像
private void RegisterDependencyResolver()
{
HttpContextBase context = new HttpContextWrapper(HttpContext.Current);
var kernel = new StandardKernel();
kernel.Bind<ISession>().To<SessionService>()
.InRequestScope()
.WithConstructorArgument("context", ninjectContext => context);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
在 application_start 方法中调用 RegisterDependencyResolver() 一样。
该接口被注入到处理会话的类的构造函数中。
问题是会话从未初始化,所以我无法向其中添加任何内容。
任何类似 context.session["something"] ="something" 的代码都会引发空引用异常。
Application_Start 在生命周期中是否太早?我认为 .InRequestScope() 解决了这个问题,但它对我不起作用。
I am injecting the httpcontext using ninject like this
private void RegisterDependencyResolver()
{
HttpContextBase context = new HttpContextWrapper(HttpContext.Current);
var kernel = new StandardKernel();
kernel.Bind<ISession>().To<SessionService>()
.InRequestScope()
.WithConstructorArgument("context", ninjectContext => context);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
RegisterDependencyResolver() is called in the application_start method.
This interface is injected into the constructor of a class that handles session.
The problem is session is never initialised so I cant add anything to it.
Any code like context.session["something"] ="something" raises a null reference exception.
Is Application_Start too early in the lifecycle? I thought .InRequestScope() fixes this but it doesnt work for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 IIS 集成模式下运行,则无法访问
Application_Start
中的任何 Http 上下文对象。尝试这样:
If you are running in IIS integrated mode you don't have access to any Http context object in
Application_Start
.Try like this: