title: How to setup the DI container in a .NET Core console app?
body: <p>I created a new .NET Core console app and installed the following packages</p>
  <ul>
  <li>Microsoft.Extensions.Configuration</li>
  <li>Microsoft.Extensions.Configuration.EnvironmentVariables</li>
  <li>Microsoft.Extensions.Configuration.Json</li>
  <li>Microsoft.Extensions.DependencyInjection</li>
  <li>Microsoft.Extensions.Options</li>
  <li>Microsoft.Extensions.Options.ConfigurationExtensions</li>
  </ul>
  <p>I created a appsettings.json file for the configuration</p>
  <pre><code>{
    &quot;app&quot;: {
      &quot;foo&quot;: &quot;bar&quot; 
    }
  }
  </code></pre>
  <p>and I want to map those values to a class</p>
  <pre><code>internal class AppOptions
  {
      public string Foo { get; set; }
  }
  </code></pre>
  <p>I also want to validate the options during configuration so I added a validating class</p>
  <pre><code>internal class AppOptionsValidator : IValidateOptions&lt;AppOptions&gt;
  {
      public ValidateOptionsResult Validate(string name, AppOptions options)
      {
          IList&lt;string&gt; validationFailures = new List&lt;string&gt;();
  
          if (string.IsNullOrEmpty(options.Foo))
              validationFailures.Add(&quot;Foo is required.&quot;);
  
          return validationFailures.Any()
          ? ValidateOptionsResult.Fail(validationFailures)
          : ValidateOptionsResult.Success;
      }
  }
  </code></pre>
  <p>I want to setup the DI container and created a testing scenario</p>
  <pre><code>    static void Main(string[] args)
      {
          ConfigureServices();
  
          Console.ReadLine();
      }
  
      private static void ConfigureServices()
      {
          IServiceCollection serviceCollection = new ServiceCollection();
          IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
  
          // Setup configuration service
  
          IConfiguration configuration = new ConfigurationBuilder()
              .AddJsonFile(&quot;appsettings.json&quot;, true, true)
              .AddEnvironmentVariables()
              .Build();
  
          serviceCollection.AddSingleton(configuration);
  
          // Setup options
  
          IConfiguration configurationFromDI = serviceProvider.GetService&lt;IConfiguration&gt;(); // This is just for testing purposes
  
          IConfigurationSection myConfigurationSection = configurationFromDI.GetSection(&quot;app&quot;);
  
          serviceCollection.AddSingleton&lt;IValidateOptions&lt;AppOptions&gt;, AppOptionsValidator&gt;();
          serviceCollection.Configure&lt;AppOptions&gt;(myConfigurationSection);
  
          // Try to read the current options
  
          IOptions&lt;AppOptions&gt; appOptions = serviceProvider.GetService&lt;IOptions&lt;AppOptions&gt;&gt;();
  
          Console.WriteLine(appOptions.Value.Foo);
      }
  </code></pre>
  <p>Unfortunately the variable <code>configurationFromDI</code> is null. So the variable <code>configuration</code> wasn't added to the DI container.</p>
  <p>How do I setup the Dependency Injection for console applications correctly?</p>