title: DI with Castle Windsor in Windows Service hosted WCF service
body: <p>I am trying to get Castle Windsor DI working with a WCF service hosted as a Windows Service. I have gone with the approach here</p>
  
  <p><a href="https://stackoverflow.com/questions/10064586/wcf-service-library-hosted-as-a-windows-service-using-castle-windsor-3-0-issue">WCF Service Library hosted as a Windows Service using Castle.Windsor 3.0 issue</a></p>
  
  <p>However the problem I have is that if my service implementation class doesn't have a default parameterless constructor, ServiceHost will not allow me to create an instance of this in OnStart(). If I provide a parameterless constructor, service console starts the service with that constructor and so I don't get any dependencies injected.</p>
  
  <p>Code below</p>
  
  <pre><code>public class WindowsService : ServiceBase
      {
          public ServiceHost ServiceHost;
  
          public WindowsService()
          {
              ServiceName = "CRMCustomerService";
          }
  
          public static void Main()
          {
              // Bootstrap the Castle Windsor DI setup
              Run(CreateContainer().Resolve&lt;ServiceBase&gt;());
          }
  
          #region Service methods
  
          protected override void OnStart(string[] args)
          {
              if (ServiceHost != null)
              {
                  ServiceHost.Close();
              }
  
              ServiceHost = new ServiceHost(typeof(CustomerService));
              ServiceHost.Open();
          }
  
          protected override void OnStop()
          {
              if (ServiceHost != null)
              {
                  ServiceHost.Close();
                  ServiceHost = null;
              }
          }
  
  
          #endregion
  
          #region Private Methods
  
          private static IWindsorContainer CreateContainer()
          {
              var container = new WindsorContainer();
              container.Install(FromAssembly.This());
              return container;
          }
  
          #endregion
      }
  
  [ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
      public class CustomerService : ICustomerService
      {
          private readonly IDataRepository _repository;
          private readonly ILogger _logger;
  
          public CustomerService(IDataRepository repository)
          {
              _repository = repository;
              _logger = new RootLogger(Level.Error);
          }
  }
  
  public class ServicesInstaller : IWindsorInstaller
      {
          public void Install(IWindsorContainer container, IConfigurationStore store)
          {
              container
                  .AddFacility&lt;WcfFacility&gt;(f =&gt;
                                                {
                                                    f.CloseTimeout = TimeSpan.Zero;
                                                })
                  .Register(
                      Component
                          .For&lt;IDataRepository&gt;()
                          .ImplementedBy&lt;CustomerRepository&gt;()
                          .LifeStyle.Transient
                          .AsWcfService(),
                      Component.For&lt;ServiceBase&gt;().ImplementedBy&lt;WindowsService&gt;());
          }
      }
  </code></pre>
  
  <p>Can anyone see what I'm doing wrong? I want to bootstrap Windsors DI container on service start up and when an instance of the WCF service is created, for it to inject the dependencies at that point.</p>