How to add new object to track in Ninject after Ap

2019-07-23 06:02发布

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

2条回答
唯我独甜
2楼-- · 2019-07-23 06:33

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-07-23 06:45

(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)

查看更多
登录 后发表回答