Table of contents
Adding Interceptors
You can associate one or more interceptors with your component by using the Interceptor attribute or a configuration node. With an interceptor you are able to pre and post process a method invocation. This is useful to manage crosscutting concerns like logging, auditing and transaction management.
An interceptor is just a standard component that implements the Castle.Core.Interceptor.IInterceptor interface. It must be registered on the container. Usually you would use a transient lifestyle for it.
Associating an interceptor with a component
You can show to the container that an interceptor should be associated with a component by using the configuration or the InterceptorAttribute.
If you want to use the configuration, see the Windsor Configuration Reference document.
To use the InterceptorAttribute simply decorate your component implementation with it. For example:
[Interceptor(typeof(LoggingInterceptor)] public class CustomerDataAccess { public virtual void Insert(Customer cust) { } }
If you are registering a component without an interface (the service) then only virtual methods can be intercepted.
Due to a bug in the DynamicProxy 1.1.5, if proxies are generated for an interface, and your component implementation exposes public properties that are not in the interface API, the MicroKernel will not be able to set those properties with values. This has been fixed on DynamicProxy 2.0.
IOnBehalfAware
The IOnBehalfAware interface allows an interceptor instance to have access to the ComponentModel of the component being intercepted. This allows the interceptor implementation to extract useful information added to the model and use them to make decisions during the method interceptions.
If you use this interface you should set your interceptor component lifestyle to transient.