Search code examples
castle-windsor

castle windsor register all components implementing specific interface


I have multiple components implementing IPollingService interface and i'd like to register them all by convention. By the way i don't see any component registered in the container and i cannot seem to resolve them. Any idea?

I tried the following but i cannot resolve them:

    public class PollingServicesInstaller : IWindsorInstaller
    { 
        public void Install( IWindsorContainer container, IConfigurationStore store )
        {
            var registrations = Classes.FromThisAssembly()
                .BasedOn<IPollingService>().WithServiceBase().AllowMultipleMatches();

            container.Register( registrations );
        }
    }

   static void Main( string[] args )
   {
        var container = new Castle.Windsor.WindsorContainer();
        container.Install( new PollingServicesInstaller() );

        var pollingServices = container.ResolveAll<IPollingService>();
        //pollingServices is empty at this point but i got several implementations in this assembly
   }

  public class ServiceMock1 : IPollingService
    {
        public int PollingInterval { get; set; }
        public bool Enabled { get; set; }

        public ServiceMock1()
        {
            this.PollingInterval = 3000;
        }

        public void Run()
        {
            Console.WriteLine( Thread.CurrentThread.ManagedThreadId );
            Console.WriteLine( "doing stuff " + this.PollingInterval );
        }
    }

As you can see from the screenshot 0 implementations are loaded.

Screenshot

I also noticed that CastleWindsor do not find my installer if i try to run installers this way:

 container.Install( FromAssembly.This() );

Solution

  • My classes and interfaces are all public but declared inside Program class, that is internal by default and so are not discovered. [See the screenshot of the code in the question]

    I think this is counterintuitive but is not necessarily a Castle.Windsor problem.

    Should Castle.Windsor behave somewhat differently?

    Why is it allowed to declare a public class inside a internal class?