开发者

Why does Castle Windsor throw a ComponentNotFoundException in this simple example?

开发者 https://www.devze.com 2023-04-08 22:10 出处:网络
I\'m just getting started with the Castle Windsor IoC, and I\'m having a hard time following the examples.Can somebody please explain why this simple console application fails?I must be missing someth

I'm just getting started with the Castle Windsor IoC, and I'm having a hard time following the examples. Can somebody please explain why this simple console application fails? I must be missing something easy. Thanks.

using System;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using Castle.Windsor.Installer;

namespace CastleTest
{
    public interface ISomething
    {
        void DoSomething();
    }

    public class Something : ISomething
    {
        public void DoSomething()
        {
            Console.WriteLine("Hello World");
        }
    }

    public class SomethingInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(AllTypes.FromThisAssembly().BasedOn<ISomething>());
        }
    }

    class Program
    {
        s开发者_开发技巧tatic void Main()
        {
            using (var container = new WindsorContainer())
            {
                container.Install(FromAssembly.This());

                // the following line throws a ComponentNotFoundException
                var something = container.Resolve<ISomething>();

                something.DoSomething();
            }
        }
    }
}


Nevermind, I found the problem.

The installer needs to register the service. This fixed it:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Register(AllTypes.FromThisAssembly().BasedOn<ISomething>()
                       .WithService.DefaultInterface()
                      );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消