views:

48

answers:

2

Is it possible to add a new object that Ninject should be responsible for (lifetime, injection etc.) in an ASP.NET application after the Application_Started event is fired?

My application needs to dynamically designate objects that should be tracked well after the application is started

+1  A: 

If you have access to the Kernel object created at Application_Start either by a static reference or [preferably] by use of a Common Service Locator, you should be able to call Bind<T>() on the object to update the context.

I haven't personally tried this, but it does comply with the class structure.

Nathan Taylor
There's nothing preferable about CSL, though an M3 CSL is OK :P
Ruben Bartelink
A: 

(Some of this addresses the other answer more than your question, but hope it helps overall) You don't really call Bind<T>() on object instances, you do that to register bindings which are then used in either resolving new instances via Kernel.Get<T>() or using Kernel.Inject(@object) to inject [typically properties only (as you're not constructing)] into an object not created under the control of Ninject.

While Injected objects will be activated etc., their scoping etc. doesnt always work in the same way. Perhaps you can expand on exactly what specific services you expect to gain other than property injection to clarify? (See the cache and collect opus for details of lifetime management)

Ruben Bartelink