10

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.

Samuel
  • 12,073
  • 5
  • 49
  • 71
  • How are you hooking Ninject into your code? Are you using Ninject.MVC3 from nuget, and then editing the NinjectWebCommon.cs file? or are you doing a custom controller factory? – Erik Funkenbusch May 14 '12 at 21:21
  • @MystereMan Thank you very much. This is exactly what I want. I used NuGet to find Ninject MVC3 and just add what you said in NinjectWebCommon for the bind of my dependencies and it works. I think the key why this work is because of these lines: DynamicModuleUtility.RegisterModule(typeof (OnePerRequestHttpModule)); and DynamicModuleUtility.RegisterModule(typeof (NinjectHttpModule)); OnePerRequestHttpModule will also dispose all my unit of work immeditaly after each request. Thank you very much! – Samuel May 15 '12 at 18:11

2 Answers2

10

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.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • What is this comment added at the beginning of the answer? – Samuel Aug 03 '13 at 11:27
  • @Samuel - I had originally posted this as a comment, but you asked me to post it as an answer so it could be marked as the answer. So I did, and you did in fact mark it as the answer. Then, for some reason, you suddenly decided to unmark it as the answer. – Erik Funkenbusch Aug 03 '13 at 21:28
7

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

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • Thank you Remo for your time. Do you have a sample of code of how to implement it. In my module, I use the bind syntax for the type I wish to be injected and with the help of a using ninject.web.common, I call at the end of the bind instruction the method InRequestScope. It not enough to tell ninject which scope I want? – Samuel May 14 '12 at 23:23
  • 1
    No that's not enough, because InRequestScope can mean InWCFRequestScope or InHttpRequestScope and in future propably other scopes as well. Web.Common does not know anything about the various request types. I strongly advice to use one of the Web extensions. There is no reason for implementing own factories and I don't give any support on them. Look at the extensions and copy all the logic from there if you really want to use your own factory. – Remo Gloor May 14 '12 at 23:30
  • Hi Remo, thank you helping me. I searched into the extensions and google 'InHttpRequestScope' but I don't found any information helping me to implement InHttpRequestScope. In which extension could I find this method, class? – Samuel May 15 '12 at 00:25
  • @Samuel Did you look in Ninject.Extension.Mvc* and Ninject.Extensions.Wcf ? – Ruben Bartelink May 15 '12 at 07:35
  • Thank you guys. Like I said to MystereMan in a comment, including reference to Ninject MVC3 with NuGet, this solved all my problems. – Samuel May 15 '12 at 18:17