我目前正在使用Castle Windsor管理我的应用程序的依赖项。
我目前正在使用Classes.FromAssemblyContaining
注册给定类型IFoo<>
的所有组件,并希望为所有这些类注册拦截器。
当我注册这样的单个组件时,我可以让拦截器工作:
public void SetUpContainer(IWindsorContainer container)
{
container.Register(Component.For<IFoo>()
.ImplementedBy<Foo>()
.LifestyleSingleton().Interceptors<MyIntercepter>());
}
但是我想继续使用Classes.FromAssembly注册。我的注册码目前看起来像这样:
public void SetUpContainer(IWindsorContainer container)
{
container.Register(Classes.FromAssemblyContaining<Foo>()
.BasedOn(typeof(IFoo<>))
.WithService.Base());
}
如何为已注册的所有IFoo<>
添加拦截器?
答案 0 :(得分:4)
您只需使用Configure
方法添加拦截器:
public void SetUpContainer(IWindsorContainer container)
{
container.Register(Classes.FromAssemblyContaining<Foo>()
.BasedOn(typeof(IFoo<>))
.WithService.Base()
.Configure(r => r.Interceptors<FooInterceptor>()));
}
我还需要注册拦截器:
container.Register(Component.For<FooInterceptor>());
但是大概是你在其他地方注册(按惯例)。