castle windsor 注册实现特定接口的所有组件
castle windsor register all components implementing specific interface
我有多个实现 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 个实现。
我还注意到,如果我尝试 运行 安装程序,CastleWindsor 找不到我的安装程序:
container.Install( FromAssembly.This() );
我的 classes 和接口都是 public 但在程序 class 中声明,默认情况下是内部的,因此不会被发现。 [见题中代码截图]
我认为这是违反直觉的,但不一定是 Castle.Windsor 问题。
Castle.Windsor 的行为是否应该有所不同?
Why is it allowed to declare a public class inside a internal class?
我有多个实现 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 个实现。
我还注意到,如果我尝试 运行 安装程序,CastleWindsor 找不到我的安装程序:
container.Install( FromAssembly.This() );
我的 classes 和接口都是 public 但在程序 class 中声明,默认情况下是内部的,因此不会被发现。 [见题中代码截图]
我认为这是违反直觉的,但不一定是 Castle.Windsor 问题。
Castle.Windsor 的行为是否应该有所不同?
Why is it allowed to declare a public class inside a internal class?