views:

810

answers:

1

I'm trying to get an interceptor I've written to work, but for some reason it doesn't seem to be instantiating the interceptor when I request my components. I'm doing something like this (forgive me if this doesn't quite compile, but you should get the idea):

container.Register(
    Component.For<MyInterceptor>().LifeStyle.Transient,
    AllTypes.Pick().FromAssembly(...).If(t => typeof(IView).IsAssignableFrom(t)).
    Configure(c => c.LifeStyle.Is(LifestyleType.Transient).Named(...).
                   Interceptors(new InterceptorReference(typeof(MyInterceptor)).
    WithService.FromInterface(typeof(IView)));

I've put breakpoints in the constructor for the Interceptor and it doesn't seem to be instantiating it at all.

In the past I've registered my interceptors using the XML configuration, but I'm keen to use the fluent interface.

Any help would be greatly appreciated!

+3  A: 

I think you're misusing WithService.FromInterface. The docs say:

Uses implements to lookup the sub interface. For example: if you have IService and IProductService : ISomeInterface, IService, ISomeOtherInterface. When you call FromInterface(typeof(IService)) then IProductService will be used. Useful when you want to register all your services and but not want to specify all of them.

You're also missing the InterceptorGroup Anywhere. Here's a working sample, I changed it as little as possible from your sample to make it work:

[TestFixture]
public class PPTests {
    public interface IFoo {
        void Do();
    }

    public class Foo : IFoo {
        public void Do() {}
    }

    public class MyInterceptor : IInterceptor {
        public void Intercept(IInvocation invocation) {
            Console.WriteLine("intercepted");
        }
    }

    [Test]
    public void Interceptor() {
        var container = new WindsorContainer();

        container.Register(
            Component.For<MyInterceptor>().LifeStyle.Transient,
            AllTypes.Pick()
                .From(typeof (Foo))
                .If(t => typeof (IFoo).IsAssignableFrom(t))
                .Configure(c => c.LifeStyle.Is(LifestyleType.Transient)
                                    .Interceptors(new InterceptorReference(typeof (MyInterceptor))).Anywhere)
                .WithService.Select(new[] {typeof(IFoo)}));

        container.Resolve<IFoo>().Do();
    }
}
Mauricio Scheffer
This works, it seems i was just missing the Anywhere on the InterceptorGroup, I guess this is just a case of me not reading the docs (out of interest, where are they?). The WithService.Select(), doesn't seem to select the interface with IView that I want, as it's implemented multiple times (don't ask), FromService seems to do the trick though.
jonnii