title: Accessing environment variables from ASP.NET Core 1 RC1 IConfiguration
body: <p>I am working on learning how to properly handle environment variables and configuring my app in multiple environments. I've chosen to read from a <em>config.json</em> in Development, and use environment variables in Production.</p>
  
  <p>I have the following <em>Startup.cs</em> to demonstrate this:</p>
  
  <pre><code>using Microsoft.AspNet.Builder;
  using Microsoft.AspNet.Hosting;
  using Microsoft.AspNet.Http;
  using Microsoft.Extensions.Configuration;
  
  namespace Variables
  {
      public class Startup
      {
          private IConfiguration mConfiguration;
  
          public Startup(IHostingEnvironment env)
          {
              var builder = new ConfigurationBuilder();
              if (env.IsDevelopment())
              {
                  // Only load from config when in development.
                  builder.AddJsonFile("config.json");
              }
              builder.AddEnvironmentVariables();
              mConfiguration = builder.Build();
          }
  
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              app.UseIISPlatformHandler();
              app.UseDeveloperExceptionPage();
  
              app.Run(async (context) =&gt;
              {
                  // Succeeds with hosting:environment=Development, fails with hosting:environment=Production
                  // ArgumentNullException: Value cannot be null. Parameter name: text
                  //  Environment variable setup in Windows with:
                  //  set bar=1
                  await context.Response.WriteAsync(mConfiguration["bar"]);
              });
          }
  
          public static void Main(string[] args) =&gt; WebApplication.Run&lt;Startup&gt;(args);
      }
  }
  </code></pre>
  
  <p>My <em>config.json</em> is simply:</p>
  
  <pre><code>{
    "bar": 1
  }
  </code></pre>
  
  <p>This succeeds when running as <code>hosting:environment=Development</code>. This fails though, when running as <code>hosting:environment=Production</code>. I set up an environment variable in Windows with <code>set bar=1</code>.</p>
  
  <p>I've also tried utilizing the system environment variables (because I'm not sure if opening up a command prompt and typing <code>set bar=1</code> does a user environment variable, or system variable), but it fails with the same error when running my app as well.</p>