Search code examples
c#castle-windsor

Castle Windsor register multiple types of a base Interface?


I know how to do this with Autofac but not with Castle.

public interface IAmBaseInterface {}
public interface IAmInterface : IAmBaseInterface {...}
public class AmClass : IAmInterface {...}

I want to register implementation AmClass (and any other implementations) using IAmBaseInterface. Is this possible? I tried the following but it didn't register the type.

container.Register(Classes.FromThisAssembly().BasedOn<IAmBaseInterface>()
    .WithServiceAllInterfaces();

Tried this too which would be similar to AutoFac, but no success.

container.Register(Classes.FromThisAssembly()
    .Where(t => typeof(IAmBaseInterface).IsAssignableFrom(t)).WithService.AllInterfaces();

Solution

  • I realized that I had used Classes.From(Type[] ...) in my code rather than Classes.FromThisAssembly as in the example. Apparently it won't scan multiple assemblies to find basedon implementations. I also found that it won't scan multiple assemblies to find internal implementations but that's a side note.