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()
);
}
精彩评论