Ninject.Web(web表单扩展),注入一个WebForm页面之外吗? [英] Ninject.Web (webforms extension), injecting outside of a webform page?
问题描述
我一直在使用Ninject.Web扩展业务对象,仓库,实体框架等方面注入到我的应用程序。这个作品非常好使用[注入]属性可以从PageBase继承一个WebForm内应用。我现在遇到了一个障碍,因为我想写一个需要注入其内,但当然这提供者未从表单中实例的做了一个自定义的成员提供。当它需要它的窗体身份验证将实例化对象。我不能确定如何对这样做,而不必访问[注入]属性。据我所知,有一个应用程序级的内核地方,但我不知道如何利用它。任何建议将不胜AP preciated。
I've been using the Ninject.Web extension to inject business objects, repositories, Entity Framework context etc into my application. This works very well using the [Inject] attribute which can be applied within a webform that inherits from PageBase. I am now running into a snag as I am trying to write a custom membership provider that needs injection done inside of it but of course this provider is not instantiated from within a webform. Forms Authentication will instantiate the object when it needs it. I am unsure how to about doing this without having access to the [Inject] attribute. I understand that there is an application level kernel somewhere, but I have no idea how to tap into it. Any suggestions would be greatly appreciated.
推荐答案
您的实例做了 IKernel.Inject
。看一看源在你使用的扩建工程中的应用类。
You do a IKernel.Inject
on the the instance. Have a look at the source for the Application class in the extension project you're using.
在V2的情况下,它在一个 KernelContainer
。所以,你需要做:
In the case of V2, it's in a KernelContainer
. So you need to do a:
KernelContainer.Inject( this )
其中,这个
是非页面,非应用程序类,其中你说话。
where this
is the non-page, non application class of which you speak.
您需要确保这只会发生一次 - 小心被全球
,这可能会多次实例这样做。
You'll need to make sure this only happens once - be careful doing this in Global
, which may get instantiated multiple times.
此外,您的应用程序/ 全球
类需要从<一个派生href=\"http://github.com/ninject/ninject.web/blob/master/source/Ninject.Web/NinjectHttpApplication.cs\"相对=nofollow> NinjectHttpAppplication ,但我敢肯定,你的覆盖。
Also, your Application / Global
class needs to derive from NinjectHttpAppplication, but I'm sure you've that covered.
这篇关于Ninject.Web(web表单扩展),注入一个WebForm页面之外吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!