title: Windows Service hosting Web API 2 via Owin Starts and Immediately Stops with no Exceptions Thrown
body: <p>The title sums up my issue. I have <code>try-catch</code> blocks around the service's <code>OnStart</code> method and the <code>Main</code> method in the service, and no exception is ever thrown. The service starts and immediately stops, with the following message:</p>
  
  <blockquote>
    <p>The Foo service on Local Computer started and then stopped. Some
    services stop automatically if they are not in use by other services
    or programs.</p>
  </blockquote>
  
  <p>The same behavior can be observed on the test server, so it is not machine-dependent. I've verified that the configuration file is present and contains the appropriate settings.</p>
  
  <p>I cannot see any obvious reason why this would be occurring. Can someone please take a look at this and shed some light on it? I've consulted other, similar questions and none of them shed any light on this particular service.</p>
  
  <p><strong>Startup.cs:</strong></p>
  
  <pre><code>public class Startup
  {
      // This code configures Web API. The Startup class is specified as a type
      // parameter in the WebApp.Start method.
      public void Configuration(IAppBuilder appBuilder)
      {
          // Configure Web API for self-host.
          var config = new HttpConfiguration();
          config.MapHttpAttributeRoutes();
          appBuilder.UseWebApi(config);
      }
  }
  </code></pre>
  
  <p><strong>WebApiService.cs:</strong></p>
  
  <pre><code>public partial class WebApiService : ServiceBase
  {
      private readonly IConfig config;
      private IDisposable webApp;
  
      public WebApiService()
      {
          try
          {
              InitializeComponent();
              this.config = UnityBootstrapper.Initialize().Resolve&lt;IConfig&gt;();
          }
          catch (Exception e)
          {
              new ApplicationEventLog(new Config()).WriteError(e);
          }
      }
  
      protected override void OnStart(string[] args)
      {
          try
          {
              var baseUrl = $"http://*:{this.config.WebApiHttpPort}";
  
              this.webApp = WebApp.Start&lt;Startup&gt;(baseUrl);
          }
          catch (Exception e)
          {
              new ApplicationEventLog(this.config).WriteError(e);
          }
      }
  
      protected override void OnStop()
      {
          this.webApp?.Dispose();
          this.webApp = null;
      }
  
      protected override void OnShutdown()
      {
          this.webApp?.Dispose();
          this.webApp = null;
      }
  }
  </code></pre>
  
  <p><strong>WebApiService.Designer.cs</strong></p>
  
  <pre><code>partial class WebApiService
  {
      /// &lt;summary&gt; 
      /// Required designer variable.
      /// &lt;/summary&gt;
      private System.ComponentModel.IContainer components = null;
  
      /// &lt;summary&gt;
      /// Clean up any resources being used.
      /// &lt;/summary&gt;
      /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
      protected override void Dispose(bool disposing)
      {
          if (disposing &amp;&amp; (components != null))
          {
              components.Dispose();
          }
          base.Dispose(disposing);
      }
  
      #region Component Designer generated code
  
      /// &lt;summary&gt; 
      /// Required method for Designer support - do not modify 
      /// the contents of this method with the code editor.
      /// &lt;/summary&gt;
      private void InitializeComponent()
      {
          components = new System.ComponentModel.Container();
          this.ServiceName = "Foo";
      }
  
      #endregion
  }
  </code></pre>
  
  <p><strong>Program.cs:</strong></p>
  
  <pre><code>public static class Program
  {
      public static bool IsConsoleApp = false;
      public static bool LogRequests = false;
  
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      public static void Main(params string[] args)
      {
          try
          {
              if (args.Length &gt; 0 &amp;&amp; args.Any(a =&gt; a.ToLower().In("l", "-l", "/l")))
              {
                  LogRequests = true;
              }
  
              if (args.Length &gt; 0
                  &amp;&amp; args[0].ToLower().In("c", "-c", "/c"))
              {
                  RunAsApp();
              }
              else
              {
                  RunAsService();
              }
          }
          catch (Exception e)
          {
              new ApplicationEventLog(new Config()).WriteError(e);
          }
      }
  
      private static void RunAsService()
      {
          Program.IsConsoleApp = false;
  
          var servicesToRun = new ServiceBase[]
                                        {
                                            new WebApiService()
                                        };
          ServiceBase.Run(servicesToRun);
      }
  
      private static void RunAsApp()
      {
          Program.IsConsoleApp = true;
  
          var config = UnityBootstrapper.Initialize().Resolve&lt;IConfig&gt;();
          var url = $"http://*:{config.WebApiHttpPort}";
  
          Console.WriteLine($@"Running. Hosted at: {url}");
          Console.WriteLine(@"Press ENTER to exit.");
  
          using (WebApp.Start&lt;Startup&gt;(url))
          {
              while (Console.ReadKey().Key != ConsoleKey.Enter)
              {
                  Thread.Sleep(0);
              }
          }
  
          Program.IsConsoleApp = false;
      }
  }
  </code></pre>
  
  <p><strong>LogController.cs:</strong></p>
  
  <pre><code>[RoutePrefix("api/log")]
  public class LogController : ApiController
  {
      private readonly ILogger logger;
      private readonly IContextFactory contextFactory;
      private readonly IConfig config;
  
      public LogController() : base()
      {
          var container = UnityBootstrapper.Initialize();
  
          this.logger = container.Resolve&lt;ILogger&gt;();
          this.config = container.Resolve&lt;IConfig&gt;();
          this.contextFactory = container.Resolve&lt;IContextFactory&gt;();
      }
  
      [HttpGet]
      [Route("Status")]
      public string Status()
      {
      }
  
      [HttpPost]
      [Route("Message")]
      public string Write([FromBody] LogMessage logMessage)
      {
          // Elided for ... reasons
      }
  
      [HttpPost]
      [Route("Performance")]
      public string Write([FromBody] PerformanceMessage performanceMessage)
      {
          // Elided for ... reasons
      }
  }
  </code></pre>
  
  <p><strong>Edit</strong>: Added WebApiService.Designer.cs, as the service shuts down when it returns from InitializeComponent.</p>
  
  <p><strong>Edit 2</strong>: I've traced it to <code>WebApp.Start&lt;Startup&gt;(baseUrl)</code> in <code>WebApiService.cs</code>. A <em>TargetInvocationException</em> is thrown, with an <em>HttpListenerException</em> as the inner exception. The message on the HttpListenerException is "Access is denied."</p>
  
  <pre><code>System.Reflection.TargetInvocationException occurred
    HResult=0x80131604
    Message=Exception has been thrown by the target of an invocation.
    Source=mscorlib
    StackTrace:
     at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
     at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(IAppBuilder builder)
     at Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer(StartContext context)
     at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context)
     at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions options)
     at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options)
     at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider services, StartOptions options)
     at Microsoft.Owin.Hosting.WebApp.Start(StartOptions options)
     at Microsoft.Owin.Hosting.WebApp.Start[TStartup](StartOptions options)
     at Microsoft.Owin.Hosting.WebApp.Start[TStartup](String url)
     at Foo.Logging.WebApi.WebApiService.OnStart(String[] args) in C:\Dev\Foo\Foo.WebApi\WebApiService.cs:line 45
  
  Inner Exception 1:
  HttpListenerException: Access is denied
  </code></pre>
  
  <p>I've researched possible causes for this, and it could be the URL or an ACL. I changed the URL to <code>http://+:{this.config.WebApiHttpPort}</code>, to no avail. I then created an ACL as follows:</p>
  
  <pre><code>netsh http add urlacl http://+:8089/ user=Everyone
  </code></pre>
  
  <p>The error still occurs.</p>