问题
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