简体   繁体   English

具有流畅接口的城堡拦截器

[英]Castle Interceptors With Fluent Interface

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. 我已经将断点放在Interceptor的构造函数中,它似乎根本没有实例化它。

In the past I've registered my interceptors using the XML configuration, but I'm keen to use the fluent interface. 过去,我已经使用XML配置注册了拦截器,但是我热衷于使用流畅的界面。

Any help would be greatly appreciated! 任何帮助将不胜感激!

I think you're misusing WithService.FromInterface . 我认为您在滥用WithService.FromInterface The docs say: 文档说:

Uses implements to lookup the sub interface. 使用工具查找子接口。 For example: if you have IService and IProductService : ISomeInterface, IService, ISomeOtherInterface. 例如:如果您有IService和IProductService:ISomeInterface,IService,ISomeOtherInterface。 When you call FromInterface(typeof(IService)) then IProductService will be used. 当您调用FromInterface(typeof(IService))时,将使用IProductService。 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 . 您还缺少InterceptorGroup AnywhereInterceptorGroup 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM