title: Could not to get environment variable in asp.net core web API 6.0
body: <p>I am working a asp .net core  web API 6.0 ( clean architecture project).</p>
  <p>This is my appsettings.Production.json and appsettings.Development.json</p>
  <pre><code>  {
  &quot;Tokens&quot;: {
      &quot;Key&quot;: &quot;my-token&quot;,
      &quot;Issuer&quot;: &quot;issuer&quot;,
      &quot;Audience&quot;: &quot;audience&quot;,
      &quot;ExpirationInMinutes&quot;: 1440
    },
  // removed rest
  }
  </code></pre>
  <pre><code>using Microsoft.Extensions.Configuration;
  
      public static IServiceCollection AddTokenAuthentication(this IServiceCollection services, IConfiguration config)
      {
          var secret = config.GetValue&lt;string&gt;(&quot;Tokens:Key&quot;);  // this value is null while running in development / production mode. 
  
          var key = Encoding.ASCII.GetBytes(secret);
           // removed rest
        }
  </code></pre>
  <p>The error I got in terminal while running in development/ production environment</p>
  <p><code>Unhandled exception. System.ArgumentNullException: String reference not set to an instance of a String. (Parameter 's') at System.Text.Encoding.GetBytes(String s) at Infrastructure.Files.AuthenticationExtension.AddTokenAuthentication(IServiceCollection services, IConfiguration config) in /src/Infrastructure/Files/AuthenticationExtension.cs:line 14 at Infrastructure.DependencyInjection.AddInfrastructure(IServiceCollection services, IConfiguration configuration) in /src/Infrastructure/DependencyInjection.cs:line 75 at Program.&lt;Main&gt;$(String[] args) in/src/WebApi/Program.cs:line 21 Aborted (core dumped)</code></p>
  <p>Note:
  If I hard-code the value of <code>secret</code> like this
  <code>var secret = &quot;my-token&quot;;</code> then the project is running fine in both environment.</p>
  <p><strong>All of the environment variables are null while running in development and production environment.</strong></p>
  <p>Same error for <code>GetConnectionString</code></p>
  <pre><code>public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
      {
  
          services.AddDbContext&lt;DbContext&gt;(options =&gt;
              options.UseNpgsql(
                  configuration.GetConnectionString(&quot;DefaultConnection&quot;),  // this is also null while running in both environment.. If I hard-coded then working fine
                  b =&gt; b.MigrationsAssembly(typeof(DbContext).Assembly.FullName))
                  .EnableSensitiveDataLogging());
  // removed rest
  }
  
  </code></pre>
  <p>Why this is happens?
  Is there any mistakes in my code?
  Please help me to find the mistake?</p>
  <p>EDIT:
  I also have tried copied all from <code>appsettings.Development.json</code> and pasted it into  <code>appsettings.json</code>
  That does not work.</p>
  <p>and here is my
  launchSettings.json</p>
  <pre><code>&quot;WebApi&quot;: {
        &quot;commandName&quot;: &quot;Project&quot;,
        &quot;dotnetRunMessages&quot;: true,
        &quot;launchBrowser&quot;: false,
        &quot;launchUrl&quot;: &quot;swagger&quot;,
        &quot;applicationUrl&quot;: &quot;http://localhost:5000&quot;,
        &quot;environmentVariables&quot;: {
          &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Development&quot;
        }
      },
      &quot;WebApi-Production&quot;: {
        &quot;commandName&quot;: &quot;Project&quot;,
        &quot;dotnetRunMessages&quot;: true,
        &quot;launchBrowser&quot;: true,
        &quot;applicationUrl&quot;: &quot;https://localhost:7152;http://localhost:5105&quot;,
        &quot;environmentVariables&quot;: {
          &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Production&quot;
        }
      },
  </code></pre>