title: Host WCF service using top shelf and windsor castle
body: <p>I am using Topshelf to run my wcf service and windsor castle for dependency injection. Problem it when I run the host program directly, it runs fine as console host but the same program gives this error while trying to start it as windows service. </p>
  
  <pre><code>Topshelf.Hosts.StartHost Error: 0 : The service failed to start., System.Invalid
  OperationException: Cannot start service MyService on computer '.'. ---&gt; Sy
  stem.ComponentModel.Win32Exception: The service did not respond to the start or
  control request in a timely fashion
     --- End of inner exception stack trace ---
     at System.ServiceProcess.ServiceController.Start(String[] args)
     at System.ServiceProcess.ServiceController.Start()
     at Topshelf.Runtime.Windows.WindowsHostEnvironment.StartService(String servic
  eName, TimeSpan startTimeOut)
     at Topshelf.Hosts.StartHost.Run()
  </code></pre>
  
  <p>Below is my implementation :-</p>
  
  <pre><code>      class Program{
         static void Main(string[] args)
         {
          var boot = new ServiceInstaller();
  
          HostFactory.Run(x =&gt;
          {
              x.Service&lt;Program&gt;(s =&gt;
              {
                  s.ConstructUsing(name =&gt; new Program());
                  s.WhenStarted(p =&gt; p.Start());
                  s.WhenStopped(p =&gt; p.Stop());
              });
              x.DependsOnMsSql();
              x.StartAutomatically();
  
              x.SetDisplayName("My Service");
              x.SetServiceName("MyService");
          });
          }
  
          public void Start()
          {
              try
              {
  
                  _serviceProvider = typeof(IMyService).AssemblyQualifiedName;
                  LogMessage(LogLevel.INFO, _serviceProvider, null);
                  if (_serviceProvider == null)
                  {
                      throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, "Failed to start service: {0}", _serviceProvider));
                  }
                  _serviceHost = new DefaultServiceHostFactory().CreateServiceHost(_serviceProvider, new Uri[0]);
                  _serviceHost.Open();
              }
              catch (Exception ex)
              {
                  LogMessage(LogLevel.ERROR, "Error occurred while starting My Service", ex);
                  throw;
              }
          }
  
  
          public void Stop()
          {
  
              try
              {
  
                  _serviceHost.Close();
  
                  _serviceHost = null;
              }
              catch (Exception ex)
              {
                  LogMessage(LogLevel.ERROR, "Error occurred while stopping My service ", ex);
                  throw;
              }
  
          }
   }
  public Class ServiceInstaller{
  public static IWindsorContainer Container { get; private set; }
  
          public ServiceInstaller()
          {
              try
              {
                  Container = new WindsorContainer();
                  Container.Register(Component.For&lt;IWindsorContainer&gt;().Instance(Container));
  
  
                  Container.AddFacility&lt;WcfFacility&gt;().Register
                  (
                       Component.For&lt;IDependency&gt;().ImplementedBy&lt;Dependency&gt;().LifestyleTransient(),
                       Component.For&lt;IMyService&gt;().ImplementedBy&lt;MyService&gt;().LifestyleTransient()
                  );
  
              }
              catch (Exception ex)
              {
                  throw;
              }
          }
  
          public void Dispose()
          {
              Dispose(true);
              GC.SuppressFinalize(this);
          }
  
          protected virtual void Dispose(bool disposing)
          {
              if (disposing)
              {
                  if (Container != null)
                  {
                      Container.Dispose();
                      Container = null;
                  }
              }
          }
      }
  </code></pre>