Asp.net mvc 4 Ninject在请求范围内失去绑定

Asp.net mvc 4 Ninject在请求范围内失去绑定,asp.net-mvc-4,dependency-injection,ninject,ninject.web.mvc,Asp.net Mvc 4,Dependency Injection,Ninject,Ninject.web.mvc,我对Ninject和MVC4有一个令人沮丧的问题 以下是代码块: Kernel.Bind<IUserInfo>().To<UserInfo().InRequestScope(); var userInfo = Kernel.Get<IUserInfo>(); 我把我想不出来的东西都删减了,现在不知所措。我不知道为什么会间歇性地失败。基于我对Ninject公认的有限知识,绑定应该不会丢失 我看到很多关于使用Ninject MVC Nuget软件包的引用,但是我继承的

我对Ninject和MVC4有一个令人沮丧的问题

以下是代码块:

Kernel.Bind<IUserInfo>().To<UserInfo().InRequestScope();
var userInfo = Kernel.Get<IUserInfo>();
我把我想不出来的东西都删减了,现在不知所措。我不知道为什么会间歇性地失败。基于我对Ninject公认的有限知识,绑定应该不会丢失

我看到很多关于使用Ninject MVC Nuget软件包的引用,但是我继承的应用程序没有使用这些,它使用ActionFilter初始化Ninject。这种模式是否只是在其核心被破坏,并以某种方式干扰了正确的绑定

帮助?

查看BindFilter选项


我认为存在某种缓存问题,这使得过滤器的行为与控制器不同。这意味着绑定可能会失败,通常是在负载较重的情况下,但这是不可预测的。

事实证明,新版本的Ninject需要更多的设置才能使InRequestScope正常工作。通过完全删除Ninject,并读取对Ninject、Ninject.Web.Common和Ninject.Web.MVC的引用,它添加了InRequestScope工作所必需的Ninject.Web.Common.cs文件


以前,它实际上是绑定InTransientScope,这意味着它将被垃圾收集,这是不确定的,这解释了我的间歇性问题。我希望在我尝试绑定到RequestScope时它会抛出异常,但这是一种生活。

您是为IUserInfo还是UserInfo解决问题?例外情况表明第一种情况。但是您的示例使用UserInfo.Oops,这是一个很好的例子。我决定为IUserInfo编辑问题。
Error activating IUserInfo
No matching bindings are available, and the Type is not self-bindable.
Activation path:
    1) Request for IUserInfo
Suggestions:
    1) Ensure that you have defined a binding for IUserInfo.
    2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
    3) Ensure you have not accidentally created more than one kernel.      
    4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.\r\n  5) If you are using automatic module loading, ensure the search path and filters are correct.