castle windsor注册所有实现特定接口的组件

时间:2015-02-27 14:58:49

标签: castle-windsor

我有多个组件实现IPollingService接口,我希望按照惯例注册它们。顺便说一句,我没有看到容器中注册的任何组件,我似乎无法解决它们。有什么想法吗?

我尝试了以下但我无法解决它们:

    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 );
        }
    }

从屏幕截图中可以看出0实现已加载。

Screenshot

我还注意到,如果我尝试以这种方式运行安装程序,CastleWindsor找不到我的安装程序:

 container.Install( FromAssembly.This() );

1 个答案:

答案 0 :(得分:1)

我的类和接口都是公共的,但在Program类中声明,默认情况下是内部的,因此不会被发现。 [请参阅问题中代码的屏幕截图]

我认为这是违反直觉的,但不一定是Castle.Windsor问题。

Castle.Windsor的行为应该有所不同吗?

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