我刚刚学习温莎城堡,并尝试使用 AOP 来解决一些问题,并希望使用拦截功能来完成此任务。
我已经弄清楚如何通过执行以下操作使其与单个组件一起工作。
container.Register(Component.For<IInterceptor>()
.ImplementedBy<BankServiceLogger>()
.Named("BankServiceInteceptor"));
container.Register(Component.For<IBankService>()
.ImplementedBy<BankService>()
.Interceptors(InterceptorReference.ForKey("BankServiceInteceptor")).Anywhere
.LifestyleTransient());
我的问题是在注册 Controller 等类时,就像我在这里所做的那样
container.Register(Classes.FromThisAssembly()
.BasedOn<IController>()
.LifestyleTransient());
似乎没有使用拦截器。
有没有办法在注册多个类时进行拦截,或者我是否需要单独注册每个组件来完成此操作?
最佳答案
Configure
BasedOnDescriptor
上的方法提供了这种能力(以及配置许多其他东西的能力),但是我刚刚意识到,因为 Configure
旨在采取 Action<ComponentRegistration>
你不能调用Anywhere
属性 InterceptorGroup
因此拦截器不会应用于组件注册。以下是我能找到的最佳解决方法。
container.Register(Component.For<MyInterceptor>());
container.Register(Classes.FromThisAssembly()
.BasedOn<IController>().WithService.FromInterface()
.LifestyleTransient()
//.Configure(c => c.Interceptors(InterceptorReference.ForType<MyInterceptor>()).Anywhere)
.Configure(delegate(ComponentRegistration c) { var x = c.Interceptors(InterceptorReference.ForType<MyInterceptor>()).Anywhere; })
);
关于c# - CaSTLe Windsor 对注册类而不是组件进行拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14107588/