gpt4 book ai didi

caSTLe-windsor - CaSTLe Windsor ArrayResolver 尝试实例化无法解析的数组依赖项

转载 作者:行者123 更新时间:2023-12-01 05:43:12 25 4
gpt4 key购买 nike

在使用 ArrayResolver 时,如果我注册了一个接口(interface)的多个实现和一个依赖于所述接口(interface)数组的类,我希望数组解析器注入(inject)所有可以成功解析的接口(interface)实现。如果该接口(interface)的注册实现无法解析,则不会将其注入(inject)依赖类(显然),并且容器不会抛出异常。

从 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 中确认了不正确的行为。

最佳答案

正如 Mauricio 指出的那样,现在已解决(添加此答案以便可以关闭问题)

关于caSTLe-windsor - CaSTLe Windsor ArrayResolver 尝试实例化无法解析的数组依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4016638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com