title: NetCore 2.1 Generic Host as a service
body: <p>I'm trying to build a Windows Service using the latest Dotnet Core 2.1 runtime. I'm NOT hosting any aspnet, I do not want or need it to respond to http requests.   </p>
  
  <p>I've followed the code found here in the samples:  <a href="https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals/host/generic-host/samples/2.x/GenericHostSample" rel="noreferrer">https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals/host/generic-host/samples/2.x/GenericHostSample</a>  </p>
  
  <p>I've also read this article:  <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.1" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.1</a></p>
  
  <p>The code works great when run inside of a console window using dotnet run.  I need it to run as a windows service.  I know there's the Microsoft.AspNetCore.Hosting.WindowsServices, but that's for the WebHost, not the generic host.  We'd use host.RunAsService() to run as a service, but I don't see that existing anywhere.</p>
  
  <p>How do I configure this to run as a service?</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Threading;
  using System.Threading.Tasks;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Hosting;
  using Microsoft.Extensions.Logging;
  
  namespace MyNamespace
  {
      public class Program
      {
  
  
          public static async Task Main(string[] args)
          {
              try
              {
                  var host = new HostBuilder()
                      .ConfigureHostConfiguration(configHost =&gt;
                      {
                          configHost.SetBasePath(Directory.GetCurrentDirectory());
                          configHost.AddJsonFile("hostsettings.json", optional: true);
                          configHost.AddEnvironmentVariables(prefix: "ASPNETCORE_");
                          configHost.AddCommandLine(args);
                      })
                      .ConfigureAppConfiguration((hostContext, configApp) =&gt;
                      {
                          configApp.AddJsonFile("appsettings.json", optional: true);
                          configApp.AddJsonFile(
                              $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
                              optional: true);
                          configApp.AddEnvironmentVariables(prefix: "ASPNETCORE_");
                          configApp.AddCommandLine(args);
                      })
                      .ConfigureServices((hostContext, services) =&gt;
                      {
                          services.AddLogging();
                          services.AddHostedService&lt;TimedHostedService&gt;();
                      })
                      .ConfigureLogging((hostContext, configLogging) =&gt;
                      {
                          configLogging.AddConsole();
                          configLogging.AddDebug();
  
                      })
  
                      .Build();
  
                  await host.RunAsync();
              }
              catch (Exception ex)
              {
  
  
  
              }
          }
  
  
      }
  
      #region snippet1
      internal class TimedHostedService : IHostedService, IDisposable
      {
          private readonly ILogger _logger;
          private Timer _timer;
  
          public TimedHostedService(ILogger&lt;TimedHostedService&gt; logger)
          {
              _logger = logger;
          }
  
          public Task StartAsync(CancellationToken cancellationToken)
          {
              _logger.LogInformation("Timed Background Service is starting.");
  
              _timer = new Timer(DoWork, null, TimeSpan.Zero,
                  TimeSpan.FromSeconds(5));
  
              return Task.CompletedTask;
          }
  
          private void DoWork(object state)
          {
              _logger.LogInformation("Timed Background Service is working.");
          }
  
          public Task StopAsync(CancellationToken cancellationToken)
          {
              _logger.LogInformation("Timed Background Service is stopping.");
  
              _timer?.Change(Timeout.Infinite, 0);
  
              return Task.CompletedTask;
          }
  
          public void Dispose()
          {
              _timer?.Dispose();
          }
      }
      #endregion
  }
  </code></pre>
  
  <p>EDIT:  I repeat, this is not to host an ASP.NET Core app.  This is a generic hostbuilder, not a WebHostBuilder.</p>