title: How to host .NET 6 Web API as Windows Service?
body: <p>I have .NET 6 Web API and want to host as a windows service on my own virtual server. I read many questions and answers and applied them, but I still get an error when starting the application <strong>(error 1053: the service did not respond to the start or control request in a timely fashion)</strong> Also I couldn't find a detailed error description when I looked in the event log.</p>
  <p>I'm using Microsoft.Extensions.Hosting.WindowsServices package and Program.cs is below</p>
  <pre><code>var builder = WebApplication.CreateBuilder(new WebApplicationOptions
  {
      Args = args,
      ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
  });
  builder.Host.UseWindowsService();
  var connectionString = builder.Configuration.GetConnectionString(&quot;MongoDB&quot;);
  ...
  </code></pre>
  <p>I'm developing on MAC so publishing my codes with the following command</p>
  <pre><code>dotnet publish --runtime win7-x64 --self-contained  --configuration Release
  </code></pre>
  <p>I'm creating my windows service like this</p>
  <pre><code>sc create &quot;MySvc&quot; binPath= &quot;C:\Users\Administrator\Desktop\test\MySvc.exe&quot;
  </code></pre>
  <p>Where am I doing wrong or how can I find the error?</p>