Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 548
Samuel Avatar asked May 14 '12 21:05

Samuel


2 Answers

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.

like image 188
Erik Funkenbusch Avatar answered Oct 05 '22 19:10

Erik Funkenbusch


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

like image 42
Remo Gloor Avatar answered Oct 05 '22 19:10

Remo Gloor