为什么温莎城堡在这个简单的示例中抛出 ComponentNotFoundException?
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我发现了问题。
安装程序需要注册该服务。这修复了它:
Nevermind, I found the problem.
The installer needs to register the service. This fixed it: