Samuel
Samuel

Reputation: 12341

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.

Upvotes: 10

Views: 4293

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

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.

Upvotes: 10

Remo Gloor
Remo Gloor

Reputation: 32725

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

Upvotes: 7

Related Questions