When does Ninject's OnActivation get fired?
I am sure this is a stupid question, as I am assuming the answer is "When the object is instantiated by Ninject"... But I want to double check...
To give a little more context as to why I am asking the question, I have an asp mvc app which implements NinjectHttpApplication
. However within the OnApplicationStarted
override I need to activate a method on a singleton instance that is bound within Ninject.
The problem is that the Kernel is exposed but shouldn't be used as a service locator, so I could get the object back from it and call DoSomeSetup();
method, but that seems like a fail. To my knowledge the NinjectHttpApplication
instance isn't DI'd as the Global.asax file uses it directly, so I couldn't DI it into the application that way.
This leaves me with one other option which would be to add the OnActivation(x=>x.DoSomeSetup());
call to the binding, however this object is only used in certain places, so it's not like the first time you load a page it would be required by the controller, s开发者_如何学编程o if OnActivation
waits for the first instance to be created it may not happen for a while.
Hopefully that's not too confusing :)
Yes your asumption is correct (or at least mostly correct). The activation actions are run the first time an instance is resolved even if not created by Ninject (e.g. Constants)
For HttpApplication you can use property injection by the way.
精彩评论