Windsor does not allow intercepted components

Can someone explain why this is not working? If you remove the interceptor from IFoo registration and enable Bar, you will get Foo (MyFoo is not null). But with the interceptor, Foo is no longer permitted.

Why? How can I explain why it will not be resolved by logging or tracing?

Versions:

  • 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();
        }
    
    }
    }
    

Edit: I just found (mostly by accident) that changing the interceptor configuration from an interface to a specific class works. However, I register the interceptor and its interface, so the original question changes slightly: why does the interface specification fail (silently, no less)?

+5
source share
1

, . , , .

, , Bar :

public class Bar : IBar
{
    public Bar(IFoo foo)
    {
        MyFoo = foo;
    }

    public virtual IFoo MyFoo { get; private set; }
}

Properties:

Component.For<IBar>().ImplementedBy<Bar>().LifestyleTransient()
    .Properties(Prop‌​ertyFilter.RequireAll)

: PropertiesRequired Properties, .

github, : -

+2

All Articles