Ninject 3 InRequestScope not returning the same instance for the same request

折月煮酒 提交于 2019-12-04 02:59:18

问题


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

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.

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.

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

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

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; }

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.

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

Thank you very much.


回答1:


Added as an answer so this can be closed out

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.




回答2:


Ninject.Web.Common can't be used standalone. You must use some additional web or wcf extension or implement InRequestScope yourself.



来源:https://stackoverflow.com/questions/10591203/ninject-3-inrequestscope-not-returning-the-same-instance-for-the-same-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!