title: AOP with Castle Windsor
body: <p>What I'm trying to achieve is AOP via attributes using Castle Windsor interceptors. I've had some success but having trouble with the aspects at the class level vs. the method level.</p>
  
  <ul>
  <li>If I use class level attributes only, all methods will be intercepted.</li>
  <li>If I use method level attributes only, those methods will be intercepted.</li>
  <li>If I add a class level attribute, and a method level attribute, both interceptions will happen on methods that were attributed, but the ones that were not will not be intercepted.</li>
  </ul>
  
  <p>So given this component:</p>
  
  <pre><code>public interface IMyComponent
  {
      void ShouldBeInterceptedByStopWatch_AND_ExceptionLogger();
      void ShouldBeInterceptedByExceptionLogger();
  }
  
  [LogExceptionAspect]
  public class MyComponent : IMyComponent
  {
      [StopwatchAspect]
      public void ShouldBeInterceptedByStopWatch_AND_ExceptionLogger()
      {
          for (var i = 0; i &lt; 2; i++) Thread.Sleep(1000);
      }
  
      public void ShouldBeInterceptedByExceptionLogger()
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>I would expect the logger aspect to intercept <code>ShouldBeInterceptedByExceptionLogger()</code> but its not happening unless I remove the stopwatch aspect from the other method, or remove the logger aspect from the class and add it to the <code>ShouldBeInterceptedByExceptionLogger()</code>. BUT the <code>ShouldBeInterceptedByStopWatch_AND_ExceptionLogger()</code> is intercepted by both.</p>
  
  <p>The entire sample application can be found at
   - <a href="https://bitbucket.org/jayala/juggernet-aop" rel="nofollow">https://bitbucket.org/jayala/juggernet-aop</a></p>
  
  <p>basically what its doing is using a facility to register a <code>IContributeComponentModelConstruction</code> that will add an interceptor if it finds an aspect at class level, or add an interceptor + method hooks if it finds aspects at method level.</p>
  
  <p>This is how I bootstrap the container:</p>
  
  <pre><code>        var container = new WindsorContainer()
              .AddFacility&lt;LogExceptionAspectFacility&gt;()
              .AddFacility&lt;StopwatchAspectFacility&gt;()
              .Register(Component
                  .For&lt;IMyComponent&gt;()
                  .ImplementedBy&lt;MyComponent&gt;()
                  .LifeStyle.Transient);
  </code></pre>
  
  <p>What the facilities are doing is registering an interceptor and a model contributor like this</p>
  
  <pre><code>public abstract class BaseAspectFacility&lt;TAspectAttribute, TAspectInterceptor&gt; : AbstractFacility
      where TAspectAttribute : Attribute
      where TAspectInterceptor : IInterceptor
  {
      protected override void Init()
      {
          var interceptorName = Guid.NewGuid().ToString();
          Kernel.Register(Component.For&lt;IInterceptor&gt;()
                                   .Named(interceptorName)
                                   .ImplementedBy&lt;TAspectInterceptor&gt;()
                                   .LifeStyle.Singleton);
          var contributor = new ContributeAspectToModel&lt;TAspectAttribute&gt;(interceptorName);
          Kernel.ComponentModelBuilder.AddContributor(contributor);
      }
  }
  </code></pre>