Unity不注入基类中定义的依赖项
我正在将Unity 2.0集成到我的ASP.NET应用程序中(使用UnityPageHandlerFactory方法),并使一切工作正常,直到我尝试将其中一个依赖项移到PageBase类中,然后该类将被所有页面共享。
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.
我正在使用描述为此处,它使用BuildUp(类型,对象)在每个页面被请求时将依赖项注入每个页面的方法。只要我具有在声明的类型中定义的属性,就将注入这些属性。但是永远不会设置基类中定义的属性。
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.
事实证明,我对BuildUp方法使用了不同的重载,并且
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.
我正在使用BuildUp(object),但无法正常工作。
I was using BuildUp(object) and it was not working. When I switched to BuildUp(Type, object), everything works like a charm!
当我切换到BuildUp(Type,object)时,一切都像一个吊饰!
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.