温莎城堡注册实现特定接口的所有组件 [英] castle windsor register all components implementing specific interface
问题描述
我有多个实现IPollingService接口的组件,我想按惯例将它们全部注册。顺便说一句,我没有在容器中看到任何组件注册,而且似乎无法解决它们。知道吗?
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 );
}
}
从屏幕截图中可以看到0个实现已加载。
As you can see from the screenshot 0 implementations are loaded.
我还注意到,如果我尝试通过这种方式运行安装程序,则CastleWindsor找不到我的安装程序:
I also noticed that CastleWindsor do not find my installer if i try to run installers this way:
container.Install( FromAssembly.This() );
推荐答案
我的类和接口都是公共的,但在Program中声明类,默认情况下是内部类,因此不会被发现。 [查看问题代码的屏幕截图]
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]
我认为这是违反直觉的,但不一定是Castle.Windsor问题。
I think this is counterintuitive but is not necessarily a Castle.Windsor problem.
Castle.Windsor的行为应该有所不同吗?
Should Castle.Windsor behave somewhat differently?
这篇关于温莎城堡注册实现特定接口的所有组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!