Ninject问题MVC3用RavenDB会话结合常数 [英] Ninject problem binding to constant value in MVC3 with a RavenDB session
问题描述
我已经看到了很多与ASP.NET MVC配置Ninject不同的方式,但执行似乎与MVC框架的每个版本略有变化。我试图注入RavenDB会话进入我的仓库。以下是我有这几乎工作。
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);
}
}
当它试图解决IDocumentSession,我碰到下面的错误。
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
这是如何使IDocumentSession决心?任何想法
Any ideas on how to make the IDocumentSession resolve?
推荐答案
ToConstant(MvcApplication.CurrentSession)
在应用程序启动评估。你想要的是延迟评估 ToMethod(CTX =&GT; MvcApplication.CurrentSession)
ToConstant(MvcApplication.CurrentSession)
is evaluated at application start. What you want is delayed evaluation ToMethod(ctx => MvcApplication.CurrentSession)
这篇关于Ninject问题MVC3用RavenDB会话结合常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!