title: The "correct" way to create a .NET Core console app without background services
body: <p>I'm building a simple .NET Core console application that will read in basic options from the command line, then execute and terminate without user interaction. I'd like to take advantage of DI, so that lead me to using the .NET Core generic host.</p>
  <p>All of the examples I've found that build a console app create a class that either implements IHostedService or extends BackgroundService. That class then gets added to the service container via AddHostedService and starts the application's work via StartAsync or ExecuteAsync. However, it seems that in all of these examples, they are implemementing a background service or some other application that runs in a loop or waits for requests until it gets shut down by the OS or receives some request to terminate. What if I just want an app that starts, does its thing, then exits? For example:</p>
  <p>Program.cs:</p>
  <pre><code>namespace MyApp
  {
      using System;
      using System.Threading.Tasks;
      using Microsoft.Extensions.DependencyInjection;
      using Microsoft.Extensions.Hosting;
      using Microsoft.Extensions.Logging;
  
      public static class Program
      {
          public static async Task Main(string[] args)
          {
              await CreateHostBuilder(args).RunConsoleAsync();
          }
  
          private static IHostBuilder CreateHostBuilder(string[] args) =&gt;
              Host.CreateDefaultBuilder(args)
                  .UseConsoleLifetime()
                  .ConfigureLogging(builder =&gt; builder.SetMinimumLevel(LogLevel.Warning))
                  .ConfigureServices((hostContext, services) =&gt;
                  {
                      services.Configure&lt;MyServiceOptions&gt;(hostContext.Configuration);
                      services.AddHostedService&lt;MyService&gt;();
                      services.AddSingleton(Console.Out);
                  });
      }
  }
  </code></pre>
  <p>MyServiceOptions.cs:</p>
  <pre><code>namespace MyApp
  {
      public class MyServiceOptions
      {
          public int OpCode { get; set; }
          public int Operand { get; set; }
      }
  }
  </code></pre>
  <p>MyService.cs:</p>
  <pre><code>namespace MyApp
  {
      using System.IO;
      using System.Threading;
      using System.Threading.Tasks;
      using Microsoft.Extensions.Hosting;
      using Microsoft.Extensions.Options;
  
      public class MyService : IHostedService
      {
          private readonly MyServiceOptions _options;
          private readonly TextWriter _outputWriter;
  
          public MyService(TextWriter outputWriter, IOptions&lt;MyServiceOptions&gt; options)
          {
              _options = options.Value;
              _outputWriter = outputWriter;
          }
  
          public async Task StartAsync(CancellationToken cancellationToken)
          {
              _outputWriter.WriteLine(&quot;Starting work&quot;);
  
              DoOperation(_options.OpCode, _options.Operand);
  
              _outputWriter.WriteLine(&quot;Work complete&quot;);
          }
  
          public async Task StopAsync(CancellationToken cancellationToken)
          {
              _outputWriter.WriteLine(&quot;StopAsync&quot;);
          }
  
          protected void DoOperation(int opCode, int operand)
          {
              _outputWriter.WriteLine(&quot;Doing {0} to {1}...&quot;, opCode, operand);
  
              // Do work that might take awhile
          }
      }
  }
  </code></pre>
  <p>This code compiles and runs just fine, producing the following output:</p>
  <pre><code>Starting work
  Doing 1 to 2...
  Work complete
  </code></pre>
  <p>However, after that, the application will just sit there waiting until I press Ctrl+C. I know I could force the application to shutdown after the work is complete, but at this point, I feel like I'm not using IHostedService correctly. It seems as though it's designed for recurring background processes, and not simple console applications like this. However, in an actual application where DoOperation might take 20-30 minutes, I would like to take advantage of the StopAsync method to do cleanup before terminating. I also know I could create the service container myself and all that, but the .NET Core generic host already does a lot of stuff I would want to do anyway. It <em>seems</em> to be the right way to write console applications, but without adding a hosted service that kicks off the actual work, how do I get the app to actually <em>do</em> anything?</p>