title: Unable to run an Asp.Net core application as Windows Service
body: <p>I'm trying to allow the execution of an ASP.NET Core 3.1 Web API as Windows Service...
  The problem I've is that the application hangs on RunAsService.</p>
  <p>Here's the code of my program.cs</p>
  <pre><code> public static void Main(string[] args)
          {
              var configuration = new ConfigurationBuilder()
                  .AddJsonFile(&quot;appsettings.json&quot;)
                  .Build();
  
              var logger = new LoggerConfiguration()
                  .ReadFrom.Configuration(configuration)
                  .CreateLogger();
  
              Log.Logger = logger;
              var app = new CommandLineApplication();
  
  
  
              app.HelpOption();
              var optionHosting = app.Option(&quot;--hosting &lt;TYPE&gt;&quot;, &quot;Type of the hosting used. Valid options: `service` and `console`, `console` is the default one&quot;, CommandOptionType.SingleValue);
              var optionPort = app.Option(&quot;--port &lt;NUMBER&gt;&quot;, &quot;Post will be used, `5000` is the default one&quot;, CommandOptionType.SingleValue);
              app.OnExecute(() =&gt;
              {
                  //
  
  
                  try
                  {
                      var hosting = optionHosting.HasValue()
                              ? optionHosting.Value()
                              : &quot;console&quot;;
  
                      var port = optionPort.HasValue()
                          ? new Func&lt;int&gt;(() =&gt;
                          {
                              if (int.TryParse(optionPort.Value(), out var number))
                              {
                                  // Returning successfully parsed number
                                  return number;
                              }
  
                              // Returning default port number in case of failure
                              return 5000;
                          })()
                          : 5000;
  
                      var builder = GetHostBuilder(args, port);
  
                      if (Debugger.IsAttached || hosting.ToLowerInvariant() != &quot;service&quot;)
                      {
                          builder
                              .UseContentRoot(Directory.GetCurrentDirectory())
                              .Build()
                              .Run();
                      }
                      else
                      {
                          logger.Information(&quot;ci arrivo&quot;);
                          var processModule = System.AppDomain.CurrentDomain.BaseDirectory;
                          logger.Information(&quot;ci arrivo2&quot;);
  
                          Directory.SetCurrentDirectory(processModule);
                          if (processModule != null)
                              builder
                                  .UseContentRoot(processModule)
                                  .Build()
                                  .RunAsService();
  
                      }
                  }
                  catch (Exception exception)
                  {
  
                      logger.Error(exception, &quot;&quot;);
                  }
              });
  
              app.Execute(args);
  
          }
  </code></pre>
  <p>And here's the <code>GetHostBuilder</code></p>
  <pre><code>  public static IWebHostBuilder GetHostBuilder(string[] args, int port) =&gt;
              WebHost.CreateDefaultBuilder(args)
                  .UseKestrel()
                  .UseIISIntegration()
                  .UseUrls($&quot;http://*:{port}&quot;)
                  .UseStartup&lt;Startup&gt;().UseSerilog();
  </code></pre>
  <p>Looking at MSDN <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.windowsservices.webhostwindowsserviceextensions.runasservice?view=aspnetcore-3.0" rel="nofollow noreferrer">here</a> it tells that</p>
  <blockquote>
  <p>Runs the specified web application inside a Windows service and blocks until the service is stopped</p>
  </blockquote>
  <h1>UPDATE 1</h1>
  <p>After doing some debug I've seen that the web api responds, but when I run it as console I got in the log</p>
  <pre><code>11:25:08 INF] Running as Console application
  [11:25:10 DBG] Hosting starting
  [11:25:10 DBG] Failed to locate the development https certificate at 'null'.
  [11:25:10 DBG] Hosting started
  [11:25:10 DBG] Loaded hosting startup assembly xxx
  Hosting environment: Development
  Content root path: C:\xxxx\xxxx
  Now listening on: http://[::]:46700
  Application started. Press Ctrl+C to shut down.
  </code></pre>
  <p>When I run as service</p>
  <pre><code>2020-05-21 11:15:25.748 +02:00 [DBG] Hosting starting
  2020-05-21 11:15:25.980 +02:00 [DBG] Failed to locate the development https certificate at 'null'.
  2020-05-21 11:15:26.022 +02:00 [DBG] Hosting started
  2020-05-21 11:15:26.022 +02:00 [DBG] Loaded hosting startup assembly xxx
  </code></pre>
  <p>No information about the hosting port and so on. The other strange thing is that it continues to write under <code>c:\windows\system32\</code> even if I set the WorkindDirectory and also the ContentRoot</p>