[英]Castle Interceptors With Fluent Interface
我正在尝试使我编写的拦截器工作,但是由于某种原因,当我请求组件时,它似乎并未实例化拦截器。 我正在做这样的事情(如果编译不好,请原谅我,但是您应该明白这个想法):
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)));
我已经将断点放在Interceptor的构造函数中,它似乎根本没有实例化它。
过去,我已经使用XML配置注册了拦截器,但是我热衷于使用流畅的界面。
任何帮助将不胜感激!
我认为您在滥用WithService.FromInterface
。 文档说:
使用工具查找子接口。 例如:如果您有IService和IProductService:ISomeInterface,IService,ISomeOtherInterface。 当您调用FromInterface(typeof(IService))时,将使用IProductService。 当您想要注册所有服务但又不想指定所有服务时很有用。
您还缺少InterceptorGroup Anywhere
的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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.