Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does Ninject's OnActivation get fired?

Tags:

ninject

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, so if OnActivation waits for the first instance to be created it may not happen for a while.

Hopefully that's not too confusing :)

like image 538
Grofit Avatar asked Nov 04 '22 13:11

Grofit


1 Answers

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.

like image 163
Remo Gloor Avatar answered Nov 15 '22 13:11

Remo Gloor