views:

30

answers:

1

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 of the one with a dependency.

+1  A: 

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/

Krzysztof Koźmic