[英]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.