title: Read appsettings.json in Main Program.cs
body: <p>First of all my main purpose is to setup the IP and Port for my application dynamically.</p>
  
  <p>I'm using <code>IConfiguration</code> to inject a json config file, like some tutorial mentioned.</p>
  
  <p>However, I can't retrieve the configuration in Program.cs, because my <code>WebHostBuilder</code> will use the StartUp and Url at the same time.</p>
  
  <p>So at the time the host build up, there is nothing in my configuration.</p>
  
  <pre><code>WebProtocolSettings settings_Web = new WebProtocolSettings();
  var host = new WebHostBuilder()
                  .UseIISIntegration()
                  .UseKestrel()
                  .UseContentRoot(Directory.GetCurrentDirectory())
                  .UseStartup&lt;Startup&gt;()
                  .UseUrls(settings_Web.Url + ":" + settings_Web.Port)
                  .Build();
  </code></pre>
  
  <p>In Startup.cs</p>
  
  <pre><code>public Startup(IHostingEnvironment env)
  {
      // Set up configuration sources.
      var builder = new ConfigurationBuilder()
          .SetBasePath(env.ContentRootPath)
          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
  
      Configuration = builder.Build();
  }
  
  public IConfigurationRoot Configuration { get; set; }
  
  public void ConfigureServices(IServiceCollection services)
  {
      // Adds services required for using options.
      services.AddOptions();
  
      var _WebProtocolSettings = Configuration.GetSection("WebProtocolSettings");
  
      // Register the IConfiguration instance
      services.Configure&lt;WebProtocolSettings&gt;(_WebProtocolSettings);
  }
  </code></pre>
  
  <p>My appsettings.json:</p>
  
  <pre><code>{
      "WebProtocolSettings": {
          "Url": "127.0.0.1",
          "Port": 5050
      }
  }
  </code></pre>
  
  <p>My WebProtocolSettings.cs:</p>
  
  <pre><code>public class WebProtocolSettings
  {
      public string Url { get; set; }
      public int Port { get; set; }
  }
  </code></pre>