title: ASP.NET MVC with WEB.API and Castle Windsor container
body: <p>I have ASP.NET MVC application with integrated Castle Windsor container. When I added ApiController, and tried to do POST, I got an error message: "Make Sure That the controller has a parameterless public constructor."  Thanks for any advice.</p>
  
  <p>Castle Windsor integration:</p>
  
  <pre><code>public class CastleControllerFactory : DefaultControllerFactory
  {
      public IWindsorContainer Container { get; protected set; }
  
      public CastleControllerFactory(IWindsorContainer container)
      {
          if (container == null)
          {
              throw new ArgumentException("container");
          }
  
          this.Container = container;
      }        
  
      protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
      {
          if (controllerType == null)
          {
              return null;
          }
  
          return Container.Resolve(controllerType) as IController;
  
      }               
  
      public override void ReleaseController(IController controller)
      {
          var disposableController = controller as IDisposable;
          if (disposableController != null)
          {
              disposableController.Dispose();
          }
  
          Container.Release(controller);
      }
  }
  
  public class DefaultCastleInstaller : IWindsorInstaller
  {
      public void Install(IWindsorContainer container, IConfigurationStore store)
      {
          container.AddFacility&lt;TypedFactoryFacility&gt;();
  
          //Functions
          //container.Register(Component.For&lt;IGetIPAddressesFromRange&gt;().ImplementedBy&lt;GetIPAddressesFromRange&gt;().LifestylePerWebRequest());
          container.Register(Component.For&lt;IUnitOfWork&gt;().ImplementedBy&lt;UnitOfWork&gt;().LifestylePerWebRequest());
  
          var contollers = Assembly.GetExecutingAssembly().GetTypes().Where(x =&gt; x.BaseType == typeof(Controller)).ToList();
          foreach (var controller in contollers)
          {
              container.Register(Component.For(controller).LifestyleTransient());
          }
  
          var apiContollers = Assembly.GetExecutingAssembly().GetTypes().Where(x =&gt; x.BaseType == typeof(ApiController)).ToList();
          foreach (var apiController in apiContollers)
          {
              container.Register(Component.For(apiController).LifestyleTransient());
          }
      }
  }
  
  public class MvcApplication : System.Web.HttpApplication
  {
      protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
          GlobalConfiguration.Configure(WebApiConfig.Register);
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
  
          //Integrace CastleWindsor
          var container = new WindsorContainer();
          container.Install(new DefaultCastleInstaller());
          var castleControllerFactory = new CastleControllerFactory(container);
          ControllerBuilder.Current.SetControllerFactory(castleControllerFactory);
      }
  }
  </code></pre>
  
  <p>ApiController:</p>
  
  <pre><code> public class UserVerificationController : ApiController
   {
     private readonly IUnitOfWork _unitOfWork;
  
     /// &lt;summary&gt;
     /// Konstruktor
     /// &lt;/summary&gt;
     /// &lt;param name="unitOfWork"&gt;&lt;/param&gt;
     public UserVerificationController(IUnitOfWork unitOfWork)
     {
         _unitOfWork = unitOfWork;
     }
  
     // GET api/&lt;controller&gt;
     public IEnumerable&lt;string&gt; Get()
     {
         return new string[] { "value1", "value2" };
     }
  }
  </code></pre>