【问题标题】:Why does Castle Windsor throw a ComponentNotFoundException in this simple example?在这个简单的例子中,为什么 Castle Windsor 会抛出 ComponentNotFoundException?
【发布时间】:2011-09-28 21:12:46
【问题描述】:

我刚刚开始使用 Castle Windsor IoC,我很难按照示例进行操作。有人可以解释为什么这个简单的控制台应用程序失败了吗?我一定错过了一些简单的东西。谢谢。

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
    {
        static void Main()
        {
            using (var container = new WindsorContainer())
            {
                container.Install(FromAssembly.This());

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

                something.DoSomething();
            }
        }
    }
}

【问题讨论】:

    标签: c# .net castle-windsor ioc-container


    【解决方案1】:

    没关系,我发现了问题。

    安装程序需要注册服务。这修复了它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 2019-03-22
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      相关资源
      最近更新 更多