为什么温莎城堡在这个简单的示例中抛出 ComponentNotFoundException?

发布于 2024-12-07 05:38:43 字数 1231 浏览 1 评论 0原文

我刚刚开始使用 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();
            }
        }
    }
}

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
    {
        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();
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2024-12-14 05:38:43

没关系,我发现了问题。

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

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

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()
                      );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文