Ninject 使用 RavenDB 会话绑定到 MVC3 中的常量值的问题
我见过很多使用 ASP.NET MVC 配置 Ninject 的不同方法,但随着 MVC 框架的每个版本的发布,实现似乎都略有变化。我正在尝试将 RavenDB 会话注入我的存储库。这是我所拥有的几乎可以工作的东西。
public class MvcApplication : NinjectHttpApplication
{
...
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new MyNinjectModule());
}
public static IDocumentSession CurrentSession
{
get { return (IDocumentSession)HttpContext.Current.Items[RavenSessionKey]; }
}
...
}
public class MyNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IUserRepository>().To<UserRepository>();
Bind<IDocumentSession>().ToConstant(MvcApplication.CurrentSession);
}
}
当它尝试解析 IDocumentSession 时,出现以下错误。
Error activating IDocumentSession using binding from IDocumentSession to constant value
Provider returned null.
Activation path:
3) Injection of dependency IDocumentSession into parameter documentSession of constructor of type UserRepository
关于如何使 IDocumentSession 解析有什么想法吗?
I've seen a lot of different ways of configuring Ninject with ASP.NET MVC, but the implementation seems to change slightly with each release of the MVC framework. I'm trying to inject a RavenDB session into my repository. Here is what I have that's almost working.
public class MvcApplication : NinjectHttpApplication
{
...
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new MyNinjectModule());
}
public static IDocumentSession CurrentSession
{
get { return (IDocumentSession)HttpContext.Current.Items[RavenSessionKey]; }
}
...
}
public class MyNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IUserRepository>().To<UserRepository>();
Bind<IDocumentSession>().ToConstant(MvcApplication.CurrentSession);
}
}
When it tries to resolve IDocumentSession, I get the following error.
Error activating IDocumentSession using binding from IDocumentSession to constant value
Provider returned null.
Activation path:
3) Injection of dependency IDocumentSession into parameter documentSession of constructor of type UserRepository
Any ideas on how to make the IDocumentSession resolve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ToConstant(MvcApplication.CurrentSession)
在应用程序启动时进行评估。你想要的是延迟评估ToMethod(ctx => MvcApplication.CurrentSession)
ToConstant(MvcApplication.CurrentSession)
is evaluated at application start. What you want is delayed evaluationToMethod(ctx => MvcApplication.CurrentSession)