Castle Windsor the default constructor is called instead of the constructor with a dependency
Given the following Code.
[TestMethod] public void CanResolveILoggerTest()
{
var Container = new Castle.Windsor.WindsorContainer();
Container.Register(
Component.For<Castle.Core.Logging.ILogger>()
.ImplementedBy<Castle.Core.Logging.TraceLogger>(),
Component.For<NeedsLogger>()
);
var blah = Container.Resolve<NeedsLogger>();
}
public class NeedsLogger
{
public NeedsLogger()
{
throw new Exception("container shouldn't resolve me");
}
Castle.Core.Logging.ILogger logger;
public NeedsLogger(Castle.Core.Logging.ILogger logger)
{
this.logger = logger;
}
}
why is the default NeedsLogger constructor is called instead 开发者_如何学JAVAof the one with a dependency.
Most likely because the logger is not ready (it's waiting for its own dependency to be provided).
I strongly suggests that instead of doing this you use Logging Facility, which will take care of registering and providing loggers for you/
精彩评论