title: Resolving from Castle Windsor container in an API controller
body: <p>I have an API controller that fires a service that uses constructor dependency injection. I would like to use the Windsor container to inject those dependencies, but I'm not sure the best practice.</p>
  
  <p>Here's part of the API controller factory:</p>
  
  <pre><code>private readonly IWindsorContainer _container;
  private readonly ServiceFactory _serviceFactory;
  ...
  public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
  {
      Arguments args = new Arguments(new { serviceFactory = _serviceFactory });
      IHttpController controller = (IHttpController)_container.Resolve(controllerType, args);
      request.RegisterForDispose(new Release(() =&gt; _container.Release(controller)));
      return controller;
  }
  ...
  </code></pre>
  
  <p>Here's part of the Service Factory:</p>
  
  <pre><code>private readonly IWindsorContainer _container;
  ...
  public IService Create(Type serviceType, ApiRequestModel apiRequest)
  {
      Arguments args = new Arguments(new { request = apiRequest });
      return (IService)_container.Resolve(serviceType, args);
  }
  ...
  </code></pre>
  
  <p>Here's part of the API controller:</p>
  
  <pre><code>private ServiceFactory _serviceFactory { get; set; }
  ...
  public object Post([FromBody]ApiRequestModel request)
  {
      ...
      Type serviceType = Assembly.Load("TestProject").GetType("TestClass");
      IService instance = _serviceFactory.Create(serviceType, request);
      ...
      _serviceFactory.Release(instance);
  }
  ...
  </code></pre>
  
  <p>The Service Factory contains an instance of the Windsor container, so is it a bad idea to expose it to the API controller? If so, what is the best alternative? Thanks.</p>