title: This service on local computer started and then stopped. some services stop automatically if then are not in use by other servces or programs
body: <p>After building installing windows service I got first error "windows could not start service on local computer error 5 access is denied" when I try to start a windows service. I resolved first error by following these steps of solution : <a href="https://stackoverflow.com/questions/11978054/cannot-start-windows-service-in-networkservice-account/11983513#11983513">Cannot Start Windows Service in NetworkService account </a> .After this the notification of first error Vanished but another notification for error appeared "This service on local computer started and then stopped. some services stop automatically if then are not in use by other servces or programs". How i can solve  this issue?</p>
  
  <p>Note :
  I have visited many posted answer but they didn't solved problem .</p>
  
  <p><a href="https://stackoverflow.com/questions/12209075/windows-service-on-local-computer-started-and-then-stopped-error">Windows service on Local Computer started and then stopped error</a></p>
  
  <p>Windows Event Viewer notification :</p>
  
  <pre><code>Service cannot be started. System.InvalidOperationException: Service 'CustomerServiceLibrary.CustomersService'  has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
     at System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreApplicationEndpoints(ServiceDescription description)
     at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
     at System.ServiceModel.ServiceHostBase.InitializeRuntime()
     at System.ServiceModel.ServiceHostBase.OnBeginOpen()
     at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
     at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
     at System.ServiceModel.Channels.CommunicationObject.Open()
     at WindowsServiceHost.WCFService.OnStart(String[] a...
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/18670476/the-service-on-local-computer-started-and-then-stopped-some-services-stop-autom">The Service on local computer started and then stopped ,Some services stop automatically if there are not in use by other services or programs</a></p>
  
  <p>Edited:</p>
  
  <pre><code>namespace WindowsServiceHost
  {
      public partial class WCFService : ServiceBase
      {
          public WCFService()
          {
              InitializeComponent();
          }
  
          private ServiceHost host = null;
          protected override void OnStart(string[] args)
          {
              System.Diagnostics.Debugger.Launch();
              host = new ServiceHost(typeof(CustomersService));
              host.Open();
          }
  
          protected override void OnStop()
          {
              if (host != null)
              {
                  host.Close();
              }
              host = null;
          }
      }
  }
  </code></pre>
  
  <p>app .config file :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
      &lt;startup&gt; 
          &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
      &lt;/startup&gt;
  &lt;/configuration&gt;
  </code></pre>