Eitan
Eitan

Reputation: 173

Intercept ALL dependencies with Castle Dynamic Proxy

I'm using Castle Dynamic Proxy interceptor in my code (for logging purpose). I know how to set the interceptor on a dependency, like that:

      container.Register(Component.For<MyService>().
           Interceptors<LoggerInterceptor>());

But how can i set this interceptor to ALL my dependencies?

Upvotes: 1

Views: 419

Answers (2)

Phil Degenhardt
Phil Degenhardt

Reputation: 7264

Before you do any registration of your components you can add a handler that will add your interceptor to every component:

        container.Kernel.ComponentRegistered += (key, handler) =>
        {
            handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(LoggingInterceptor)));
        };

Upvotes: 2

Jan Muncinsky
Jan Muncinsky

Reputation: 4408

Use convention registration:

container.Register(Classes.FromThisAssembly()
                          .Pick()
                          .WithService.Self()
                          .Configure(r => r.Interceptors<LoggerInterceptor>()));

Upvotes: 1

Related Questions