gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:48 25 4
gpt4 key购买 nike

我刚刚学习 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 上的属性(property)因此拦截器不会应用于组件注册。以下是我可以解决的最佳解决方法。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com