Unity not injecting dependencies defined in base class
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 dependenci开发者_运维知识库es 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.
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.
Can you show the relevant parts of you code? This is what I have and this seems to work:
class InjectedClass
{
}
class MyBase
{
[Dependency]
public InjectedClass Dependency { get; set; }
}
class MyClass : MyBase
{
}
class Program
{
static void Main(string[] args)
{
UnityContainer uc = new UnityContainer();
uc.RegisterType<InjectedClass>();
MyClass m = new MyClass();
uc.BuildUp(m);
}
}
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.
精彩评论