Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable the default Castle interceptor on a single class?

On an application I am working on we are using some of the AOP facilities from Castle Windsor, mainly to log method entry/exit points and times for debugging/performance analysis purposes. This works fine except that because we are using a default interceptor across the whole container, it applies itself to everything, including the Logging classes, so the log ends up getting clogged with messages explaining how it is entering and exiting the log writing methods.

On other classes I am able to use a custom IInterceptorSelector along with the Intercept attribute on the class in order to override the default, but looking through the Castle documentation I could not see any equivalent DoNotIntercept type of attribute. I have created a NullInterceptorSelector for my logging classes that returns an empty list, but that seems like a crazy kind of workaround and I am sure there must be a better way to do this- can anyone suggest how?

Edit: Also the interceptor lists are cumulative, so my NullInterceptorSelector doesn't actually work.

like image 677
glenatron Avatar asked Jan 31 '26 22:01

glenatron


1 Answers

The simple solution would obviously be to not wire facilities with interceptors... but if that is not possible for some reason, you can use a InterceptorSelector like you suggested yourself.

The purpose of an IInterceptorSelector class is to provide an extension point that allows proxies to choose specific interceptors on a per method basis.

Your registration would look like this:

container.Register(Component.For<IInterface>()
         .ImplementedBy<Component>()
         .Interceptors(InterceptorReference.ForType<LoggingInterceptor>()).SelectedWith(new ExampleInterceptionSelector()).Anywhere);

Where a barebone implementation of ExampleInterceptionSelector would look like this:

public class ExampleInterceptionSelector : IInterceptorSelector
{
    public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
    {
        return new IInterceptor[];
    }
}

But a better solution would be to not wire interceptors to components that don't need them.

like image 61
Ron Sijm Avatar answered Feb 03 '26 12:02

Ron Sijm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!