我试图让我写工作的拦截器,但由于某种原因,它似乎并没有被实例化拦截时,我要求我的部件。我在做这样的事情(原谅我,如果这并不完全编译,但你应该得到的想法):

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)));

我已经把断点的构造函数拦截,它似乎并没有在所有实例化它。

在过去,我已经注册使用XML配置我的拦截器,但我很热衷于使用流利的接口。

任何帮助将不胜感激!

有帮助吗?

解决方案

我觉得你滥用WithService.FromInterface。文档说:

  

使用工具来查找子   接口。例如:如果你有   IService和IProductService:   ISomeInterface,IService,   ISomeOtherInterface。你打电话时   FromInterface(typeof运算(IService)),然后   IProductService将被使用。有用   当你想注册的所有的你   服务,但不希望指定   所有这些。

你还缺少InterceptorGroup Anywhere。 这里的工作示例,我改变了它尽可能少地从你的样品,使其工作:

[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归因
不隶属于 StackOverflow
scroll top