castle windsor register all components implementing specific interface

不想你离开。 提交于 2019-12-24 14:07:45

问题


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.

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

 container.Install( FromAssembly.This() );

回答1:


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?



来源:https://stackoverflow.com/questions/28767992/castle-windsor-register-all-components-implementing-specific-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!