c# - CaSTLe Windsor 对注册类而不是组件进行拦截

标签 c# castle-windsor

我刚刚学习温莎城堡,并尝试使用 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/

相关文章:

c# - 为什么在 Log4Net 中命名记录器?

c# - CurrentCellDirtyStateChanged 提交得太早

c# - 这个 MVC 功能叫什么?

asp.net - NInject 可以在中等信任托管中工作吗?

c# - ASP.NET MVC3 Controller AOP 代理不拦截所有方法,只有 IController.Execute

c# - 温莎城堡 : Auto-register types from one assembly that implement interfaces from another

c# - CaSTLe Windsor - 一个实现多个接口(interface)的类

c# - ASP.net MVC 将单元格内容作为 Grid.MVC 中的链接

c# - ViewModel 中的文本框事件处理

c# - 折叠行时动态调整数据网格列的大小