- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在使用 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
最佳答案
正如 Mauricio 指出的那样,现在已解决(添加此答案以便可以关闭问题)
关于caSTLe-windsor - CaSTLe Windsor ArrayResolver 尝试实例化无法解析的数组依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4016638/
我在调用 Resolve 时遇到异常: KernelException: Could not instantiate custom activator Inner Exception: {"Const
我的应用程序使用“SignalR”客户端/服务器通信框架。如果您不熟悉它,服务器端应用程序通常包含一个或多个“集线器”类(类似于 asmx 网络服务),每个类都提供可由客户端调用的方法。在启动期间,客
假设我有这样的组件 public class MyComponent { public MyComponent(string name) { } } 我基本上想让提供的构造函数
在 CaSTLe Windsor 场景中,我想检查我的容器是否注册了某个服务,并且基本上做 if (container.HasComponentFor()) { // resolve serv
我想知道是否有一些最佳实践来实现我需要的功能。 我有一个 Web 应用程序,它在启动期间通过反射扫描某些程序集(插件)并针对公共(public)内核注册它们的依赖项。 外部库可能需要相同的依赖项。 例
我想知道是否有一些最佳实践来实现我需要的功能。 我有一个 Web 应用程序,它在启动期间通过反射扫描某些程序集(插件)并针对公共(public)内核注册它们的依赖项。 外部库可能需要相同的依赖项。 例
是否有关于如何使用 CaSTLe Windsor 的自动事务管理的任何简单示例? documentation似乎缺少一些信息。我看到有用于 nHibernate、ActiveRecord 等的工具……
考虑这个例子: public class Factory { private List subFactories; public Factory(Lis
我正在将我们的项目从 .Net 2 升级到 .Net4.5,同时我将尽可能多的引用推送到 NuGet 并确保版本是最新的。 我在运行其中一项测试时遇到问题 测试类: public cl
当我开始使用 Windsor 时,我认为 DI 会很简单。现在它让我越来越困惑。 在我看来,存储库是一个具有单例生命周期的类。我应该有一个 FooRepository 实例来在应用程序的生命周期内将
我有一个接口(interface) ISession,其实例由不同的 session 工厂生成,具体取决于类所属的命名空间。 我的组件注册示例: IWindsorContainer container
我有一个看似简单的用例。有一个 ICsvReader 组件。让我们在这里简单地将它命名为 Reader。我们加载了一组已知的 CSV 文件,其中一些有标题,有些没有。目前有多个阅读器:Reader_S
这是我在 Global.asax 中的代码 WindsorContainer container = new WindsorContainer(); container.Register(Compo
在使用 ArrayResolver 时,如果我注册了一个接口(interface)的多个实现和一个依赖于所述接口(interface)数组的类,我希望数组解析器注入(inject)所有可以成功解析的接
有没有人有一些使用城堡 Windsor InstallerFactory 来订购安装程序的示例代码? 似乎无法在文档或其他地方找到它。 干杯 最佳答案 您只能使用 InstallerFactory与
documentation 中有说明你应该总是让拦截器 transient 。如果我有这个示例代码; //register interceptor container.Register(Classes
我有一点问题。我在 CaSTLe Windsor IOC Container 工作。现在我想做的只是弄乱一些 AOP 原则,而我特别想做的是基于方法名称执行一些日志记录。我一直在研究拦截器,目前我正在
有人对使用 CaSTLe DynamicProxy 拦截属性的更好方法有什么建议吗? 具体来说,我需要我正在拦截的 PropertyInfo,但它不是直接在 IInvocation 上,所以我要做的是
使用 ArrayResolver 时,如果我注册了多个实现相同接口(interface)的依赖项,并且我已经注册了依赖于这些依赖项数组的类,那么人们会期望 ServiceOverrides 受到尊重并
如何使用 Windsor 容器将 appSettings 条目的值(来自 app.config 或 web.config)注入(inject)服务?如果我想将 Windsor 属性的值注入(injec
我是一名优秀的程序员,十分优秀!