5
任何人都可以解释为什么这不工作?如果你从IFoo的注册中删除拦截器并解析一个Bar,你会得到一个Foo(MyFoo不为空)。但随着拦截器,Foo不再解决。温莎没有解决拦截组件
为什么?我怎么知道为什么它不能通过日志或跟踪来解决?
版本:
- Castle.Core:3.2
- Castle.Windsor:3.2
- .NET 4.5
C#5
using Castle.DynamicProxy; using Castle.MicroKernel.Registration; using Castle.Windsor; using System; namespace Sandbox { public interface IFooInterceptor : IInterceptor { } public interface IFoo { void Print(); } public interface IBar { IFoo MyFoo { get; set; } } public class Foo : IFoo { public void Print() { Console.WriteLine("Print"); } } public class FooInterceptor : IFooInterceptor, IInterceptor { public void Intercept(IInvocation invocation) { Console.WriteLine("Awesome"); invocation.Proceed(); } } public class Bar : IBar { public virtual IFoo MyFoo { get; set; } } class Program { static void Main(string[] args) { IWindsorContainer container = new WindsorContainer() .Register( Component.For<IBar>().ImplementedBy<Bar>().LifestyleTransient(), Component.For<IFoo>().ImplementedBy<Foo>().LifestyleTransient().Interceptors<IFooInterceptor>(), Component.For<IFooInterceptor>().ImplementedBy<FooInterceptor>().LifestyleTransient() ); var bar = container.Resolve<IBar>(); var foo = container.Resolve<IFoo>(); // this isn't null bar.MyFoo.Print(); // exception: bar.MyFoo is null Console.WriteLine("Done"); Console.ReadLine(); } } }
编辑:我刚刚发现(主要是偶然),将拦截器配置从接口更改为具体类。但是,我正在注册拦截器及其接口,因此原来的问题稍作修改:为什么接口规范失败(默默无闻)?
我与取消Dynamicproxy标记的不以为然。 – Amy 2013-03-20 21:23:57
看来这是一个bug。礼物是可选的依赖项,但它们应该默认填入,但它与截取以某种方式冲突。如果你让你的依赖强制使用'Component.For().ImplementedBy ().LifestyleTransient()。属性(PropertyFilter.RequireAll)'它也适用。我在github上发现了这个问题:https://github.com/castleproject/Windsor/issues/24,感觉与此有关。 –
nemesv
2013-03-20 21:30:50
@nemesv我认为你可以将其作为答案发布,以便问题不会得到解决。提供Bar.MyFoo作为构造函数参数也可以解决问题。 – Marwijn 2013-03-21 07:39:40