Ninject 3 InRequestScope不相同请求返回相同的实例 [英] Ninject 3 InRequestScope not returning the same instance for the same request

查看:143
本文介绍了Ninject 3 InRequestScope不相同请求返回相同的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我升级我的MVC3项目之一,从Ninject 2至3 Ninject

Recently, I upgraded one of my MVC3 projects from Ninject 2 to Ninject 3.

几分钟试图找到为什么InRequestScope是不再可用之后,我发现这是现在Ninject.Web.Common的延伸。

After a couple of minutes trying to find why InRequestScope was not anymore available, I found that this is now an extension of Ninject.Web.Common.

现在,当我尝试运行应用程序,Ninject就像如果一个范围InRequest将InTransientScope绑定所有类型;一个新的实例每次被创建。

Now, when I try to run the application, Ninject works like if all types binded with a scope InRequest would be InTransientScope; a new instance was created each time.

在我的课,从NinjectModule继承,我有一个简单的绑定那样:

In my class that inherits from NinjectModule, I have a simple bind like that:

Bind<ViewModel.Activity>().ToSelf().InRequestScope();

在我的控制器,我打上Ninject属性类型ViewModel.Activity 2属性。

In my controller, I have 2 properties of the type ViewModel.Activity marked with Ninject attribute.

  [Inject]
  public ViewModel.Activity Activity { get; set; }

  [Inject]
  public ViewModel.Activity Activity1 { get; set; }

如果我在调试模式下看着两个两个属性的散列code的值,有都具有不同的值,但HttpContext的是相同的;我在同样的要求。

If I looked in debug mode the value of the HashCode of both the two properties, there all have different value but HttpContext is the same; I'm in the same request.

我错过了什么如何正确使用新Ninject.Web.Common.InRequestScope与Ninject 3的新版本?

What I missed about how to use correctly the new Ninject.Web.Common.InRequestScope with the new version of Ninject 3?

非常感谢你。

推荐答案

添加为答案所以这可以被关闭退出

不要使用自定义的工厂。只要安装Ninject.MVC3及以上的NinjectWebCommon.cs文件复制您的绑定,然后删除所有旧code。

Don't use a custom factory. Just install Ninject.MVC3 and copy your bindings over to the NinjectWebCommon.cs file, then delete all your old code.

这篇关于Ninject 3 InRequestScope不相同请求返回相同的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆