Ninject 3 InRequestScope not returning the same instance for the same request
Asked Answered
I

2

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.

Interpretation answered 14/5, 2012 at 21:18 Comment(2)
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?Berl
@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!Interpretation
B
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.

Berl answered 15/5, 2012 at 18:13 Comment(2)
What is this comment added at the beginning of the answer?Interpretation
@Interpretation - 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.Berl
E
7

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

Eyelet answered 14/5, 2012 at 23:9 Comment(5)
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?Interpretation
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.Eyelet
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?Interpretation
@Interpretation Did you look in Ninject.Extension.Mvc* and Ninject.Extensions.Wcf ?Clachan
Thank you guys. Like I said to MystereMan in a comment, including reference to Ninject MVC3 with NuGet, this solved all my problems.Interpretation

© 2022 - 2024 — McMap. All rights reserved.