使用ArrayResolver时,如果我注册了接口的多个实现和依赖于所述接口数组的类,我希望数组解析器注入可以成功解析的所有接口实现.如果无法解析该接口的已注册实现,则不会将其注入依赖
从Windsor 2.5.1开始,ArrayResolver将在这种情况下抛出异常,但前提是它尝试实例化的类有多个构造函数.
我不确定这种行为的改变是否是故意的.我在2.5.1破坏性更改文档中没有看到任何可以让我相信这是故意的.
请参阅以下测试用例:
using System; using Castle.MicroKernel.Registration; using Castle.MicroKernel.Resolvers.SpecializedResolvers; using Castle.Windsor; using NUnit.Framework; namespace CastleTests { [TestFixture] public class TestArrayResolver { /// <summary> /// In this test, we register two implementors of IDependency, and a "DependsOnArray" class /// that depends on an array of IDependency. The "UnresolvalbeDependency" cannot be /// resolved (because its dependency on "IUnimplementedDependency" is not satisfied) /// Hence, only the "ResolvableDependency" is injected into "DependsOnArray" /// </summary> [Test] public void ArrayResolution_UnresolvableDependencyIsNotIncluded() { using (var container = new WindsorContainer()) { container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel, false)); container.Register( Component.For<IDependency>().ImplementedBy<ResolvableDependency>(), Component.For<IDependency>().ImplementedBy<UnresolvalbeDependency>(), Component.For<IDependOnArray>().ImplementedBy<DependsOnArray>() ); container.Resolve<IDependOnArray>(); } } /// <summary> /// This test is identical to the above, except instead of registering "UnresolvableDependency" /// we register "UnresolvalbeDependencyWithPrimitiveConstructor", which is identical to /// "UnresolvableDependency", except for the fact that it has a second constructor acceptin a string. /// /// I would expect this test to behave the same way as the above test, which it does in windsor 2.5, /// however in 2.5.1 and later, it fails with the following error message: /// /// Castle.MicroKernel.ComponentActivator.ComponentActivatorException : Could not find resolvable constructor for CastleTests.UnresolvalbeDependencyWithAdditionalConstructor. Make sure all required dependencies are provided. /// </summary> [Test] public void ArrayResolution_UnresolvableDependencyCausesResolutionFailure() { using (var container = new WindsorContainer()) { container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel, true)); container.Register( Component.For<IDependency>().ImplementedBy<ResolvableDependency>(), Component.For<IDependency>().ImplementedBy<UnresolvalbeDependencyWithPrimitiveConstructor>(), Component.For<IDependOnArray>().ImplementedBy<DependsOnArray>() ); container.Resolve<IDependOnArray>(); } } /// <summary> /// this test fails with the same error as the above test, indicating that the issue is not related to the /// primitive vs. service nature of the second cosntructor's parameter /// </summary> [Test] public void ArrayResolution_UnresolvableDependencyCausesResolutionFailure_ServiceConstructor() { using (var container = new WindsorContainer()) { container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel, true)); container.Register( Component.For<IDependency>().ImplementedBy<ResolvableDependency>(), Component.For<IDependency>().ImplementedBy<UnresolvalbeDependencyWithAdditionalServiceConstructor>(), Component.For<IDependOnArray>().ImplementedBy<DependsOnArray>() ); container.Resolve<IDependOnArray>(); } } } public interface IDependency { } public class ResolvableDependency : IDependency { } public interface IUnimplementedDependency { } public class UnresolvalbeDependency : IDependency { public UnresolvalbeDependency(IUnimplementedDependency unimplementedDependency) { } } public interface IOtherUnimplementedDependency { } public class UnresolvalbeDependencyWithAdditionalServiceConstructor : IDependency { public UnresolvalbeDependencyWithAdditionalServiceConstructor(IUnimplementedDependency unimplementedDependency) { } public UnresolvalbeDependencyWithAdditionalServiceConstructor(IOtherUnimplementedDependency dep) { } } public class UnresolvalbeDependencyWithPrimitiveConstructor : IDependency { public UnresolvalbeDependencyWithPrimitiveConstructor(IUnimplementedDependency unimplementedDependency) { } public UnresolvalbeDependencyWithPrimitiveConstructor(string str) { } } public interface IDependOnArray { } public class DependsOnArray : IDependOnArray { public DependsOnArray(params IDependency[] dependencies) { foreach (var dependency in dependencies) { Console.WriteLine(dependency.GetType().Name); } } } }
下面的完整堆栈跟踪如下:
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.SelectEligibleConstructor(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs: line 288 at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs: line 104 at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs: line 85 at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\ComponentActivator\AbstractComponentActivator.cs: line 75 at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Lifestyle\SingletonLifestyleManager.cs: line 50 at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Handlers\DefaultHandler.cs: line 61 at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context, Boolean instanceRequired) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Handlers\AbstractHandler.cs: line 770 at Castle.MicroKernel.Handlers.AbstractHandler.TryResolve(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Handlers\AbstractHandler.cs: line 376 at Castle.MicroKernel.DefaultKernel.TryResolveComponent(IHandler handler, Type service, IDictionary additionalArguments) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\DefaultKernel.cs: line 899 at Castle.MicroKernel.DefaultKernel.ResolveAll(Type service, IDictionary arguments) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\DefaultKernel_Resolve.cs: line 293 at Castle.MicroKernel.Resolvers.SpecializedResolvers.ArrayResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Resolvers\SpecializedResolvers\ArrayResolver.cs: line 71 at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Resolvers\DefaultDependencyResolver.cs: line 239 at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArgumentsCore(ConstructorCandidate constructor, Object[] arguments, CreationContext context, Type[] signature) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs: line 367 at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context, Type[]& signature) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs: line 344 at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs: line 107 at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs: line 85 at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\ComponentActivator\AbstractComponentActivator.cs: line 75 at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext context) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Lifestyle\SingletonLifestyleManager.cs: line 50 at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Handlers\DefaultHandler.cs: line 61 at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context, Boolean instanceRequired) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Handlers\AbstractHandler.cs: line 770 at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\DefaultKernel.cs: line 880 at Castle.Windsor.WindsorContainer.Resolve(Type service) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\Windsor\WindsorContainer.cs: line 889 at Castle.Windsor.WindsorContainer.Resolve() in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\Windsor\WindsorContainer.cs: line 978 at CastleTests.TestArrayResolver.ArrayResolution_UnresolvableDependencyCausesResolutionFailure() in TestArrayResolver.cs: line 54
我已经确认了2.5中的正确(无异常抛出)行为,并确认了2.5.1中的错误行为,以及主干版本2146和2137.
毛里西奥指出这一点现已修复(添加此答案以便可以关闭该问题)