这个问题之前已经提过很多次了,但对于我来说,我无法让Ninject处理InRequestScope的服务。我查看了所有的答案,其中大部分都告诉用户使用Ninject.MVC3
或OnePerRequestHttpModule
。我正在使用Ninject.MVC5
。不支持InRequestScope的Ninject依赖关系
这里是如何得到的地方,我:
使用VS2013
- 添加新的项目(ASP.NET Web应用程序)
- 选择MVC和没有认证
- 添加的NuGet参考到Ninject.MVC5
添加一个接口到项目:
public interface IBlah : IDisposable { void DoSomething(); }
添加一个实现该接口:
public class Blah : IBlah { private static int _count; public Blah() { _count++; Debug.WriteLine("Blah nr {0} created", _count); } public void DoSomething() { Debug.WriteLine("DoSomething called"); } public void Dispose() { _count--; Debug.WriteLine("Blah disposed. Still left {0}", _count); } }
打开家居控制器,并添加以下实例变量和构造函数:
private readonly IBlah _blah; public HomeController(IBlah blah) { _blah = blah; }
以下内容添加到
Index
行动:_blah.DoSomething();
打开NinjectWebCommon和以下行添加到
RegisterServices
方法kernel.Bind<IBlah>().To<Blah>().InRequestScope();
调试项目,并期待在调试输出,你会看到(系统消息中)在浏览器中
"Blah nr 1 created" "DoSomething called"
点击刷新,你会请参阅
"Blah nr 2 created" "DoSomething called"
没有获得输出处理,也没有得到输出处理内部处理从未被击中。
我的测试解决方案可以在这里下载:http://www.upload.ee/files/4044477/NinjectTest.zip.html
到底我做错了什么???
UPDATE 2014年5月15日
现在有一个可用的NuGet已经修复了这一错误Ninject.Web.Common
包的新版本(3.2.2)。
你可能是正确的!谢谢! –