Question

I can't seem to register my Castle Windsor objects by convention and I'm really at a loss. The situation is I have two projects, Website (a really basic web forms project) and BusinessObjects (a class library). I'm attempting to use IoC to be able to keep all of my business object implementations internal, and only deal with the interfaces in the Website project.

The best way that I've found to accomplish this is to use installers. So, in my Global.asax.cs I have this:

private IWindsorContainer _container;

public override void Init()
{
    base.Init();

    InitializeIoC();
    Global.WindsorContainer = _container;
}

private void InitializeIoC()
{
    _container = new WindsorContainer();
    _container.Install(new BusinessObjectsInstaller());
}

public static IWindsorContainer WindsorContainer { get; private set; }

Which seems to work just fine, and to pull an object out of the container, I'm just using a very simple:

var thingey = Global.WindsorContainer.Resolve<IThingey>();

And then in my BusinessObjects project, I have this:

public class BusinessObjectsInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component.For<IThingey>().ImplementedBy<Thingey>());
    }
}

Now, at this point, everything is working as I expect it to. It's not elegant or anything, but I'm still trying to get a handle on this. So the big thing I'm trying to accomplish at this point is to wire these objects up in a more useful way, which lead me to replacing the above component registration with this:

container.Register(Classes
        .FromThisAssembly()
        .BasedOn<IThingey>()
        .LifestyleTransient()
        );

Which I absolutely cannot get to work. All I get is

No component for supporting the service BusinessObjects.Contracts.IThingey was found

Ultimately the class registration will be changed to another interface that the others inherit from, but one step at a time.

Any help figuring what's going on / what I'm doing wrong would be greatly appreciated.

Was it helpful?

Solution

You are not specifying any service that are registered by your classes, so by default the classes are registered as services to themselves. From the documentation:

By default the service of the component is the type itself

You must specify what services the component is registered against; you do that using the WithService property or shortcut functions (WithServiceBase(), WithServiceDefaultInterfaces(), etc). The linked resource contains the different selections methods you can use:

Krzysztof Kozmic is recommending that you register your components using the Base service, which you would do like this:

container.Register(Classes
    .FromThisAssembly()
    .BasedOn<IThingey>()
    .WithServiceBase()
    .LifestyleTransient()
    );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top