title: Merging appsettings with environment variables in .NET Core
body: <p>I am running a .NET Core app in Docker (in Kubernetes), passing environment variables to the Docker container and using them in my app.</p>
  
  <p>In my .NET Core app I have the following C# class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class EnvironmentConfiguration
  {
      public string EXAMPLE_SETTING { get; set; }
      public string MY_SETTING_2 { get; set; }
  }
  </code></pre>
  
  <p>And I setup my <code>appsettings</code> as such:</p>
  
  <pre class="lang-cs prettyprint-override"><code>config.
      AddJsonFile("appsettings.json").
      AddJsonFile($"appsettings.docker.json", true).
      AddEnvironmentVariables();  
  </code></pre>
  
  <p>DI setup:</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.Configure&lt;EnvironmentConfiguration&gt;(Configuration);
  </code></pre>
  
  <p>And in my Controller I use it as such:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[ApiVersion("1.0")]
  [Route("api/v{version:apiVersion}/my")]
  public class MyController : Controller
  {
      private readonly IOptions&lt;EnvironmentConfiguration&gt; _environmentConfiguration;
  
      public MyController(IOptions&lt;EnvironmentConfiguration&gt; environmentConfiguration)
      {
          _environmentConfiguration = environmentConfiguration;
      }
  }       
  </code></pre>
  
  <p>I run docker:</p>
  
  <pre><code>docker run -p 4000:5000 --env-file=myvariables
  </code></pre>
  
  <p>The file <code>myvariables</code> looks like this:</p>
  
  <pre><code>EXAMPLE_SETTING=example!!!
  MY_SETTING_2=my-setting-2!!!!
  </code></pre>
  
  <p>This works. I can use my <code>_environmentConfiguration</code> and see that my variables are set. </p>
  
  <p>However... I would like to merge environment variables with appsettings so that the values from appsettings are used as fallback when environment variables are not found. Somehow merging these two lines:</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.Configure&lt;EnvironmentConfiguration&gt;(settings =&gt; Configuration.GetSection("EnvironmentConfiguration").Bind(settings));
  services.Configure&lt;EnvironmentConfiguration&gt;(Configuration);
  </code></pre>
  
  <p>Is this somehow possible?</p>
  
  <p>My fallback plan is to inherit from the <code>EnvironmentConfiguration</code> class and use a separate DI to have two separate configurations injected and then merge them "manually" in code but this solution is undesirable.</p>