title: The service on Local Computer started then stopped
body: <p>I've created a Service that I've installed on several machines. They all work fine except for one. When I try to start the service via Windows Services I get the following error:</p>
  
  <pre><code>The My.Service service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.
  </code></pre>
  
  <p>I've seen other posts suggesting you change the service's log on properties to log on as a local system account - I'm already doing this. </p>
  
  <p>Event viewer gives me this information:</p>
  
  <pre><code>Application: myProject.exe
  Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.AggregateException
  Stack:
     at Microsoft.AspNetCore.Server.Kestrel.KestrelServer.Start[[Microsoft.AspNetCore.Hosting.Internal.HostingApplication+Context, Microsoft.AspNetCore.Hosting, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](Microsoft.AspNetCore.Hosting.Server.IHttpApplication`1&lt;Context&gt;)
     at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start()
     at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(Microsoft.AspNetCore.Hosting.IWebHost, System.Threading.CancellationToken, System.String)
     at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(Microsoft.AspNetCore.Hosting.IWebHost)
     at Nexus.Startup.Main(System.String[])
  </code></pre>
  
  <p>I've tried running the service manually from a command prompt and passing in --debug and received this error message:</p>
  
  <pre><code>09:05:47.289 [1] ERROR - 
  System.AggregateException: One or more errors occurred. ---&gt; System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Listener.&lt;DisposeAsync&gt;d__13.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.ListenerPrimary.&lt;DisposeAsync&gt;d__20.MoveNext()
     --- End of inner exception stack trace ---
     at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)
     at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout)
     at System.Threading.Tasks.Task.WaitAll(Task[] tasks, TimeSpan timeout)
     at Microsoft.AspNetCore.Server.Kestrel.Internal.KestrelEngine.DisposeListeners(List`1 listeners)
     at Microsoft.AspNetCore.Server.Kestrel.Internal.KestrelEngine.CreateServer(ServerAddress address)
     at Microsoft.AspNetCore.Server.Kestrel.KestrelServer.Start[TContext](IHttpApplication`1 application)
     at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start()
     at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host, CancellationToken token, String shutdownMessage)
     at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
     at MyProject.Startup.Main(String[] args) in D:\bamboo-home\xml-data\build-dir\163841\CUT-CUP-CR\repos\myProject\Startup.cs:line 42
  ---&gt; (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Listener.&lt;DisposeAsync&gt;d__13.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.ListenerPrimary.&lt;DisposeAsync&gt;d__20.MoveNext()&lt;---
  </code></pre>
  
  <p>I'm fairly confused since this service starts up perfectly fine on other machines. </p>
  
  <p><strong>Here is my Startup.cs main method:</strong></p>
  
  <pre><code>        public static void Main(string[] args)
      {
          var exePath = Process.GetCurrentProcess().MainModule.FileName;
          var directoryPath = Path.GetDirectoryName(exePath);
  
          if (Debugger.IsAttached || args.Contains("--debug"))
          {
              var host = new WebHostBuilder()
                 .CaptureStartupErrors(true)
                 .UseKestrel()
                 .UseUrls("http://localhost:5000")
                 .UseContentRoot(Directory.GetCurrentDirectory())
                 .UseIISIntegration()
                 .UseStartup&lt;Startup&gt;()
                 .Build();
              host.Run();
          }
          else
          {
              var host = new WebHostBuilder()
                  .UseKestrel()
                  .UseUrls("http://localhost:5000")
                  .UseContentRoot(directoryPath)
                  .UseIISIntegration()
                  .UseStartup&lt;Startup&gt;()
                  .Build();
              host.RunAsService();
          }
      }
  </code></pre>