Castle Interceptors With Fluent Interface

前端 未结 1 386
旧巷少年郎
旧巷少年郎 2020-12-20 09:18

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 s

相关标签:
1条回答
  • 2020-12-20 09:28

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题