title: ASP.NET MVC - setting custom IControllerFactory has no effect
body: <p>I really dont understand what I'm doing wrong here - for some reason the <code>IControllerFactory</code> i register is not being used, and I end up with a <code>System.ArgumentException</code>:</p>
  
  <blockquote>
    <p>Type 'Company.WebApi.Controllers.AController' does not have a default
    constructor</p>
    
    <p>at System.Linq.Expressions.Expression.New(Type type) at
    System.Web.Http.Internal.TypeActivator.Create[TBase](Type
    instanceType) at
    System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage
    request, Type controllerType, Func`1&amp; activator)  at
    System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage
    request, HttpControllerDescriptor controllerDescriptor, Type
    controllerType)</p>
  </blockquote>
  
  <p>In Global.asax.cs</p>
  
  <pre><code>protected void Application_Start()
  {
      //DI-setup
      var container = new WindsorContainer().Install(new WebWindsorInstaller());
  
      //set custom controller factory
      var controllerFactory = container.Resolve&lt;IControllerFactory&gt;();
      ControllerBuilder.Current.SetControllerFactory(controllerFactory);
  
      //register cors
      container.Resolve&lt;ICorsConfig&gt;().RegisterCors(GlobalConfiguration.Configuration);
  
      //routes
      RegisterRoutes(RouteTable.Routes);
  }
  </code></pre>
  
  <p>My <code>IControllerFactory</code> is based on this article <a href="http://keyvan.io/custom-controller-factory-in-asp-net-mvc" rel="nofollow noreferrer">http://keyvan.io/custom-controller-factory-in-asp-net-mvc</a> and is implemented as following</p>
  
  <pre><code>public class ControllerFactory : IControllerFactory
      {
          private readonly IWindsorContainer _container;
  
          public ControllerFactory(IWindsorContainer container)
          {
              if(container == null)
                  throw new ArgumentNullException("container");
  
              _container = container;
          }
  
          public IController CreateController(RequestContext requestContext, string controllerName)
          {
              if (string.IsNullOrEmpty(controllerName))
                  throw new ArgumentNullException("controllerName");
  
              var componentName = GetComponentNameFromControllerName(controllerName);
  
              return _container.Resolve&lt;IController&gt;(componentName);
          }
  
          public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
          {
              throw new NotImplementedException();
          }
  
          public void ReleaseController(IController controller)
          {
              _container.Release(controller);
          }
  
          static string GetComponentNameFromControllerName(string controllerName)
          {
              var controllerNamespace = typeof (CrudController&lt;&gt;).Namespace;
              return string.Format("{0}.{1}Controller", controllerNamespace, controllerName);
          }
      }
  </code></pre>
  
  <p>I've been staring at this for hours, and really can't see why this isn't working. When debugging the <code>ControllerFactory</code> is never hit in any method except the constructor. Anyone see whats wrong or missing here?</p>
  
  <p>(<a href="https://stackoverflow.com/questions/4952187/setting-asp-net-mvc-controllerfactory-has-no-effect">Setting ASP.NET MVC ControllerFactory has no effect</a> does not answer my question)</p>
  
  <p><strong>Edit 1 - Controller</strong></p>
  
  <pre><code>    public class AController : CrudController&lt;AModel&gt;
      {
          public AController(IAHandler aHandler) : base(aHandler)
          {
              ...
          }
  
          [HttpGet]
          public HttpResponseMessage GetByUser(string aId)
          {
              ...
          }
      }
  
      public abstract class CrudController&lt;T&gt; : ApiController
          where T : IModel, new()
      {
          protected CrudController(ICrudHandler&lt;T&gt; handler)
          {
              ...
          }
  
          [HttpGet]
          public HttpResponseMessage Get(string id)
          {
              ...
          }
  
          [HttpPost]
          public HttpResponseMessage Post(T input)
          {
              ...
          }
  
          [HttpPut]
          public HttpResponseMessage Put(T input)
          {
              ...
          }
      }
  </code></pre>
  
  <p><strong>Edit 2 - Installer</strong></p>
  
  <pre><code>    public sealed class WebWindsorInstaller : IWindsorInstaller
      {
          private bool _installComplete;
  
          public void Install(IWindsorContainer container, IConfigurationStore store)
          {
              if(_installComplete)
                  return;
  
              //register self for reuse
              container.Register(Component.For&lt;IWindsorInstaller&gt;().Instance(this));
  
              //controller factory
              container.Register(
                  Component.For&lt;IControllerFactory&gt;().ImplementedBy&lt;ControllerFactory&gt;().LifeStyle.Singleton);
  
              //Handlers
              container.Register(
                  Classes.FromAssemblyContaining&lt;AHandler&gt;().InSameNamespaceAs&lt;AHandler&gt;().WithService.
                      DefaultInterfaces());
  
              //Models
              container.Register(
                  Classes.FromAssemblyContaining&lt;AModel&gt;().InSameNamespaceAs&lt;AModel&gt;().WithService.Self());
  
              //DI
              container.Register(Component.For&lt;IWindsorContainer&gt;().Instance(container));
              container.Register(Component.For&lt;IDependencyResolver&gt;().ImplementedBy&lt;WindsorDependencyResolver&gt;());
  
              //Controllers
              container.Register(Classes
                  .FromAssemblyContaining&lt;AController&gt;()
                  .BasedOn&lt;IHttpController&gt;()
                  .LifestyleScoped());
  
              //repositories
              container.Register(
                  Classes.FromAssemblyContaining&lt;ARepository&gt;().InSameNamespaceAs&lt;ARepository&gt;().WithService.
                      DefaultInterfaces());
  
              //cors
              container.Register(Component.For&lt;ICorsConfig&gt;().ImplementedBy&lt;CorsConfig&gt;());
  
              _installComplete = true;
          }
      }
  </code></pre>