castle-windsor

Castle Windsor: How to register internal implementations


This registration works when all the implementations of IService are public:

AllTypes
    .Of<IService>()
    .FromAssembly(GetType().Assembly)
    .WithService.FirstInterface()

For example:

public interface IService {}
public interface ISomeService : IService {}
public class SomeService : ISomeService {}

Resolving ISomeService returns an instance of SomeService.

But if the SomeService class is internal, the container throws an exception that states that ISomeService is not registered.

So is there any way to register internal implementations for public interfaces?


Solution

  • UPDATE2: While I consider this not a good idea, you can achieve that with the following code.

    AllTypes.FromAssembly(GetType().Assembly)
        .IncludeNonPublicTypes()
        .BasedOn<IService>()
        .WithService.FirstInterface();
    

    UPDATE: This is a very logical behavior and I would consider this a bug if it was otherwise. AllTypes exposes only public type, because it assumes, if you made your type internal, you did it for a reason.

    When you register type explicitly on the other hand, it works because Windsor assumes, since you're explicitly asking it to use an internal type, you know better.