title: Autofac class interception doesn't work in a certain setup
body: <p>I have an IoC setup with Autofac and use AoP interceptors.</p>
  
  <p>Normally, I use interface interceptors registered like this:</p>
  
  <pre><code>var builder = new ContainerBuilder();
  builder.RegisterType&lt;MyType&gt;()
      .As&lt;IMyType&gt;()
      .UsingConstructor(new Type[0])
      .EnableInterfaceInterceptors()
      .InterceptedBy(typeof(MyInterceptor));
  </code></pre>
  
  <p>And it works. But for certain reasons (not apparent in this minimal example), I need one class to be registered and injected also as self (not through interface), so I try:</p>
  
  <pre><code>var builder = new ContainerBuilder();
  builder.RegisterType&lt;MyType&gt;()
      .As&lt;IMyType&gt;()
      .AsSelf()
      .UsingConstructor(new Type[0])
      .EnableClassInterceptors()
      .InterceptedBy(typeof(MyInterceptor));
  </code></pre>
  
  <p>In this setup, the interceptor is never fired. When I inspect the injected dependency in debug, it does seem to be a subclassed proxy (as it should), but its <code>_interceptors</code> private property only contains a single instance of <code>Castle.DynamicProxy.StandardInterceptor</code>, which is obviously not what I configured.</p>
  
  <p>In fact, if I remove <code>AsSelf()</code> it still doesn't intercept, which leads me to a conclusion that either I'm doing something wrong, or class interception simply doesn't work... ?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>MyType is actually inheriting from EF <code>DbContext</code> and I'm trying to intercept <code>SaveChanges()</code>, which is virtual. Just for test I added <code>public virtual void Foo()</code> which also wasn't intercepted.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I overlooked this earlier and to simplify skipped an important fact: I am specifying <code>UsingConstructor()</code> in the registration.
  Now I empirically found out that <code>UsingConstructor()</code> seems to prevent <code>EnableClassInterceptors()</code> from working.</p>
  
  <p>Full registration I'm currently using:</p>
  
  <pre><code>    builder.RegisterType&lt;FooClass&gt;()
          .AsSelf()
          .InstancePerRequest()
          .EnableClassInterceptors()
          .UsingConstructor(new Type[0]) // commenting this out solves the issue
          .InterceptedBy(typeof(MyInterceptor));
  
  public class FooClass
  {
      public virtual void Bar()
      {
          Debugger.Break();
      }
      public FooClass() { }
      public FooClass(int i) { }
  }
  </code></pre>
  
  <p>Interceptor works for other injections; it's complex code but I put breakpoint at start of <code>public void Intercept(IInvocation invocation)</code> method.</p>
  
  <p>Commenting out the constructor choice makes it work again. </p>
  
  <p>I will award the bounty to anyone who can give me a workaround, or at least a good explanation of why this doesn't work.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>With your answers about added constructor parameters, I investigated this aspect and indeed:</p>
  
  <pre><code>Foo.GetType().GetConstructors() // Foo is injected, proxied instance of FooClass
  </code></pre>
  
  <p>returns in fact <strong>3</strong> constructors:</p>
  
  <pre><code>Void .ctor(Castle.DynamicProxy.IInterceptor[])
  Void .ctor(Castle.DynamicProxy.IInterceptor[], Int32)
  Void .ctor()
  </code></pre>
  
  <p>This happens whether I add <code>UseConstructor()</code> or not. By coincidence, the code didn't throw for me; it would if I specified the other constructor (not the parameterless).</p>
  
  <p>Regardless, I tried the following registration:</p>
  
  <pre><code>    builder.RegisterType&lt;MyType&gt;()
          .As&lt;IMyType&gt;()
          .AsSelf()
          .InstancePerRequest()
          .UsingConstructor(new[] { typeof(IInterceptor[]) })
          .EnableClassInterceptors()
          .InterceptedBy(typeof(MyInterceptor));
  </code></pre>
  
  <p>... and, surprise, surprise! it <strong>fails</strong> with </p>
  
  <blockquote>
    <p>No matching constructor exists on type MyType</p>
  </blockquote>
  
  <p>Digging further, changing the order of those fluent methods solves it ultimately, with the complete working registration being:</p>
  
  <pre><code>    builder.RegisterType&lt;MyType&gt;()
          .As&lt;IMyType&gt;()
          .AsSelf()
          .InstancePerRequest()
          .EnableClassInterceptors()
          .UsingConstructor(new[] { typeof(IInterceptor[]) })
          .InterceptedBy(typeof(MyInterceptor));
  </code></pre>
  
  <p><strong>SUMMARY</strong></p>
  
  <p>I would consider it bad API to have what seems to be fluent API but yet depend significantly on the order of invoking fluent methods. It's even worse that it doesn't actually throw, just <em>fails silently</em>. </p>
  
  <p>I would also consider it bad design (leave alone documentation) to require from users internal knowledge of proxying logic. Ideally, UsingConstructor() should encapsulate in the matching logic the added fact of interceptors. That said, I designed APIs myself and I know it's easy to demand but difficult to deliver certain features.</p>
  
  <p>Anyway, the case is closed and I would like to give hug to all of you. I believe it was Jim Bolla that gave first precise answer that led to the breakthrough, so cookies go to him. Correct me if I'm wrong.</p>