title: Castle Windsor 3 Interceptor not releasing components created by a typed factory but 2.5.4 did. Why?
body: <p>This is a similar pattern to ones stated elsewhere and detailed <a href="http://kozmic.pl/2010/03/11/advanced-castle-windsor-ndash-generic-typed-factories-auto-release-and-more/" rel="nofollow">in this blog post</a>. I have this working using Windsor 2.5.4 pretty much as stated in the blogpost, but decided to switch to using Windsor 3. When I did this I noticed that the memory usage of the application go up over time - I guessed this would be that components were'nt being released.</p>
  
  <p>There were a couple of modifications to the code in the blogpost, which may have  caused the behaviour to differ.</p>
  
  <p>Here is my AutoRelease interceptor (straight out of the blogpost, here for convenience and the lazy ;) )</p>
  
  <pre><code>[Transient]
  public class AutoReleaseHandlerInterceptor : IInterceptor
  {
      private static readonly MethodInfo Execute = typeof(IDocumentHandler).GetMethod("Process");
      private readonly IKernel _kernel;
      public AutoReleaseHandlerInterceptor(IKernel kernel)
      {
          _kernel = kernel;
      }
  
      public void Intercept(IInvocation invocation)
      {
          if (invocation.Method != Execute)
          {
              invocation.Proceed();
              return;
          }
          try
          {
              invocation.Proceed();
          }
          finally
          {
              _kernel.ReleaseComponent(invocation.Proxy);
          }
      }
  }
  </code></pre>
  
  <p>One of my deviations from the blog post is the selector that the typed factory uses:-</p>
  
  <pre><code>public class ProcessorSelector : DefaultTypedFactoryComponentSelector
  {
      protected override Func&lt;IKernelInternal, IReleasePolicy, object&gt; BuildFactoryComponent(MethodInfo method,
                                                                                             string componentName,
                                                                                             Type componentType,
                                                                                             IDictionary additionalArguments)
      {
          return new MyDocumentHandlerResolver(componentName,
              componentType,
              additionalArguments,
              FallbackToResolveByTypeIfNameNotFound,
              GetType()).Resolve;
      }
      protected override string GetComponentName(MethodInfo method, object[] arguments)
      {
          return null;
      }
      protected override Type GetComponentType(MethodInfo method, object[] arguments)
      {
          var message = arguments[0];
          var handlerType = typeof(IDocumentHandler&lt;&gt;).MakeGenericType(message.GetType());
          return handlerType;
      }
  }
  </code></pre>
  
  <p>What might be noticeable is that I do not use the default resolver. (This is where, perhaps, the problem lies...).</p>
  
  <pre><code>public class MyDocumentHandlerResolver : TypedFactoryComponentResolver
  {
  
      public override object Resolve(IKernelInternal kernel, IReleasePolicy scope)
      {
          return kernel.Resolve(componentType, additionalArguments, scope);
      }
  }
  </code></pre>
  
  <p>(I omitted the ctor for brevity- nothing special happens there, it just calls the base ctor).</p>
  
  <p>The reason I did this was because the default resolver would try to resolve by name and not by Type- and fail. In this case, I know I only ever need to resolve by type, so I just overrode the Resolve method.</p>
  
  <p>The final piece of the puzzle will be the installer.</p>
  
  <pre><code>container.AddFacility&lt;TypedFactoryFacility&gt;()
           .Register(
            Component.For&lt;AutoReleaseHandlerInterceptor&gt;(),
            Component.For&lt;ProcessorSelector&gt;().ImplementedBy&lt;ProcessorSelector&gt;(),
            Classes.FromAssemblyContaining&lt;MessageHandler&gt;()
                   .BasedOn(typeof(IDocumentHandler&lt;&gt;))
                   .WithService.Base()
                   .Configure(c =&gt; c.LifeStyle.Is(LifestyleType.Transient)),
            Component.For&lt;IDocumentHandlerFactory&gt;()
                     .AsFactory(c =&gt; c.SelectedWith&lt;ProcessorSelector&gt;()));
  </code></pre>
  
  <p>Stepping through the code, the interceptor <em>is</em> called and the finally clause is executed (e.g. I didn't get the method name wrong). However, the component does not seem to be released (using the performance counter shows this. Each invocation of the factory's create method increases the counter by one).</p>
  
  <p>So far, my workaround has been to add a void Release(IDocumentHandler handler) method to my factory interface, and then after it executes the handler.Process() method, it explicitly releases the handler instance, and this seems to do the job- the performance counter goes up, and as the processing is done, it goes down).</p>
  
  <p>Here is the factory:</p>
  
  <pre><code>public interface IDocumentHandlerFactory
  {
      IDocumentHandler GetHandlerForDocument(IDocument document);
      void Release(IDocumentHandler handler);
  }
  </code></pre>
  
  <p>And here is how I use it:</p>
  
  <pre><code>IDocumentHandlerFactory handler = _documentHandlerFactory.GetHandlerForDocument(document);
  handler.Process();
  _documentHandlerFactory.Release(handler);
  </code></pre>
  
  <p>Doing the Release explicitly therefore negates the need for the interceptor, but my real question is why this behaviour differs between the releases?</p>