title: Intercept a class instance with Castle Windsor
body: <p>I'm trying to implement interceptor on an instantiated class, but no methods are intercepted in this case.
  All works fine with normally registered components, <code>Castle Windsor</code> intercepts all the methods of my component.</p>
  
  <p>I know that by design <code>Castle Windsor</code> cannot intercept a registered instantiated class, but I was saw this post and tried to implement it with no luck:
  <a href="https://stackoverflow.com/questions/12162769/castle-windsor-cannot-intercept-component-registered-for-a-class-instance">Castle Windsor Cannot Intercept Component Registered For A Class Instance</a></p>
  
  <p>This is my current implementation :</p>
  
  <p>My Factory Interface :</p>
  
  <pre class="lang-cs prettyprint-override"><code>public interface ITransferProtocolFactory
  {
      ITransferProtocol GetTransferProtocol(string transferProtocolName);
  }
  </code></pre>
  
  <p>Registration of the factory:</p>
  
  <pre class="lang-cs prettyprint-override"><code>IocManager.IocContainer.AddFacility&lt;TypedFactoryFacility&gt;();
  IocManager.IocContainer.Register(Component.For&lt;ITransferProtocolFactory&gt;().AsFactory(new NamedTypeFactory()));
  IocManager.IocContainer.Register(Component.For&lt;NamedTypeFactory, ITypedFactoryComponentSelector&gt;());
  </code></pre>
  
  <p>Override of the <code>GetComponentName</code> method :</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class NamedTypeFactory : DefaultTypedFactoryComponentSelector 
  {
      protected override string GetComponentName(MethodInfo method, object[] arguments)
      {
          string componentName = null;
          if (arguments != null &amp;&amp; arguments.Length &gt; 0)
          {
              componentName = arguments[0] as string;
          }
          if (string.IsNullOrEmpty(componentName))
              componentName = base.GetComponentName(method, arguments);
          return componentName;
      }
  }
  </code></pre>
  
  <p>Registering my interceptor :</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static class InterceptorsRegistrar
  {
      public static void Initialize(IKernel kernel)
      {
          kernel.ComponentRegistered += Kernel_ComponentRegistered;
      }
  
      private static void Kernel_ComponentRegistered(string key, IHandler handler)
      {
          if (typeof(ITransferProtocol).IsAssignableFrom(handler.ComponentModel.Implementation) &amp;&amp;  
                  !typeof(IProxyTransferUserAction).IsAssignableFrom(handler.ComponentModel.Implementation))
          {
              handler.ComponentModel.Interceptors.Add
                  (new InterceptorReference(typeof(TransferUserActionInterceptor)));
          }
      }
  }
  
  InterceptorsRegistrar.Initialize(IocManager.IocContainer.Kernel);
  </code></pre>
  
  <p>Creation of instance and registration :</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void CreateAndInjectMachineProtocol(MachineForAgentDto machine)
  {
      var machineProtocol = (ITransferProtocol)Activator.CreateInstance(Type.GetType("Agent.Protocols." + Enum.GetName(typeof(eCommunicationType), machine.CommunicationType)));
  
      _iocManager.IocContainer.Register(Component.For&lt;ITransferProtocol&gt;().Instance(machineProtocol).Named(nameof(ITransferProtocol) + machine.Id.ToString()).LifestyleSingleton());
  }
  </code></pre>
  
  <p>The Interceptor :</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class TransferUserActionInterceptor : IInterceptor
  {
      private readonly IMachineManager _machineManager;
  
      public TransferUserActionInterceptor(IMachineManager machineManager)
      {
          _machineManager = machineManager;
      }
  
      public void Intercept(IInvocation invocation)
      {
          invocation.Proceed();
      }
  }
  </code></pre>
  
  <p>And finally the resolver of the component through the factory:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private ITransferProtocol GetMachineProtocol(long machineId)
  {
      return _transferProtocolFactory.GetTransferProtocol(nameof(ITransferProtocol) + machineId.ToString());
  }
  </code></pre>
  
  <p>The Interception is not working in this case because of the design limitation of <code>Castle Windsor</code>.</p>
  
  <p>I have trid to implement this solution <a href="https://stackoverflow.com/questions/12162769/castle-windsor-cannot-intercept-component-registered-for-a-class-instance">Castle Windsor Cannot Intercept Component Registered For A Class Instance</a> but the interception continue not to working (maybe I have implemented it wrong).</p>
  
  <p>So here are my 2 questions :</p>
  
  <p>How can I implement the stackoverflow post solution on top of my current code to let methods of the <code>ITransferProtocol</code> injected instance be intercepted?</p>
  
  <p>Do you have another solution that the stackoverflow post suggest?</p>
  
  <p>Thanks for your help.</p>