Unity未注入基类中定义的依赖项
我正在将 Unity 2.0 集成到我的 ASP.NET 应用程序中(使用 UnityPageHandlerFactory 方法),并且一切正常,直到我尝试将依赖项之一移至 PageBase 类中,然后由所有页面共享该类。调用 BuildUp 时永远不会设置此属性。
我正在使用描述的 UnityPageHandlerFactory 类 此处,它使用 BuildUp(type, object) 方法在请求时将依赖项注入到每个页面中。只要我在声明的类型中定义了属性,这些属性就会被注入。但在基类中定义的属性永远不会被设置。
我还需要做些什么吗?在我看来,这应该是自动的。
I am integrating Unity 2.0 into my ASP.NET application (using the UnityPageHandlerFactory approach) and have everything working great until I tried to move one of the dependencies into a PageBase class that would then be shared by all pages. This property is never set when BuildUp is called.
I'm using the UnityPageHandlerFactory class described here which uses the BuildUp(type, object) method to inject dependencies into each page when it is requested. As long as I have the properties defined in the declared type, the properties are injected. But properties defined in a base class are never set.
Is there something else I need to do? It seems to me that this should be automatic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,我使用的是 BuildUp 方法的不同重载,并且使用引用示例中的方法解决了我的问题。
我正在使用 BuildUp(object) 但它不起作用。当我切换到 BuildUp(Type, object) 时,一切都像魅力一样!
我不确定为什么,但只能假设它与第一次重载中解析类型的方式有关,而不是与显式提供类型时发生的情况有关。
不管怎样,这个小小的改变解决了我所有的问题。
It turns out that I was using a different overload of the BuildUp method and going with the one in the cited example fixed my problem.
I was using BuildUp(object) and it was not working. When I switched to BuildUp(Type, object), everything works like a charm!
I'm not sure why but can only assume that it has something to do with the way the type is resolved in the first overload as opposed to what happens when the type is explicitly provided.
Either way, making this little change fixed all of my problems.
你能展示你的代码的相关部分吗?这就是我所拥有的,这似乎有效:
我还在 asp.net 应用程序中使用 UnityPageHandlerFactory 对此进行了测试,并且类似地可以看到 InjectedClass 被注入到我的页面中,尽管依赖属性位于基类上。
Can you show the relevant parts of you code? This is what I have and this seems to work:
I also tested this with UnityPageHandlerFactory in asp.net application and similarly can see that InjectedClass is injected into my page, although the dependency property is on the base class.