我刚开始使用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();
}
}
}
}
答案 0 :(得分:2)
没关系,我发现了问题。
安装程序需要注册该服务。这修好了它:
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromThisAssembly().BasedOn<ISomething>()
.WithService.DefaultInterface()
);
}