Interceptors do not work as I expected.

13 views
Skip to first unread message

mynkow

unread,
Oct 12, 2011, 9:38:49 AM10/12/11
to castle-pro...@googlegroups.com
Please, look the code below. The Green test is OK because the interceptor is called but in the Red test the interceptor is skipped. Why?

Red test outputs : Executing context...
Green test outputs : Interceptor is called.

namespace TestWindsor
{
    [TestFixture]
    public class TestingInterceptors
    {
        [Test]
        public void Green()
        {
            var container = new WindsorContainer();
            container.Register(
                Component.For<SimpleInterceptor>(),
                Component.For<IContext>().ImplementedBy<Context>().LifeStyle.Transient.Interceptors<SimpleInterceptor>()
                );
 
            var context = container.Resolve<IContext>();
            context.Execute();
        }
 
        [Test]
        public void Red()
        {
            var container = new WindsorContainer();
            container.Register(
                Component.For<SimpleInterceptor>(),
                Component.For<Context>().LifeStyle.Transient.Interceptors<SimpleInterceptor>()
                );
 
            var context = container.Resolve<Context>();
            context.Execute();
        }
    }
 
    public interface IContext
    {
        void Execute();
    }
 
    public class Context : IContext
    {
        public void Execute()
        {
            Console.WriteLine("Executing context...");
        }
    }
 
    public class SimpleInterceptor : Castle.DynamicProxy.IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            Console.WriteLine("Interceptor is called.");
        }
    }
}

mynkow

unread,
Oct 12, 2011, 9:40:30 AM10/12/11
to castle-pro...@googlegroups.com
PS: I am using ver. 2.5.1.0

Krzysztof Koźmic

unread,
Oct 12, 2011, 10:09:12 AM10/12/11
to castle-pro...@googlegroups.com
That's the expected behaviour.

The method on the class is not virtual and cannot be intercepted.

Krzysztof
--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/6XZPHpC8IK0J.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

mynkow

unread,
Oct 12, 2011, 10:11:07 AM10/12/11
to castle-pro...@googlegroups.com
I tried with virtual method and there is no difference. Same behavior. 

mynkow

unread,
Oct 12, 2011, 10:12:25 AM10/12/11
to castle-pro...@googlegroups.com
Sorry, I made the wrong method virtual. Now should be OK, 10x

Reply all
Reply to author
Forward
0 new messages