title: AWS .NET Core 3.1 Mock Lambda Test Tool, cannot read AppSettings.json or App.config
body: <p>Using the AWS .NET Core 3.1 Mock Lambda Test Tool, I cannot get the lambda function to read from an appsettings.json or even an app.config file.</p>
  <p>That is two sperate methods that when I try to get a return value, each method returns null.</p>
  <p>In a separate .NET Core 3.1 console app, these same methods work perfectly fine.</p>
  <p>So, is there some reason why the 'Mock Lambda Test Tool' at runtime will not allow my code to read from a JSON or App.config file set to copy-always? And does this mean this will not run on AWS when packaged and uploaded to the AWS Lambda console?</p>
  <p>My situation does <em>not</em> allow me to use Lambda Environment Variables for my local DB connection string. And I cannot store the connection string inside the code, as it has to come from a .json or .config file.</p>
  <p>Any ideas or wisdom on this?</p>
  <p>THE CODE:</p>
  <p><strong>METHOD 1</strong></p>
  <pre><code>// requires: System.Configuration.ConfigurationManager
  var connString = System.Configuration.ConfigurationManager.AppSettings[&quot;connectionString&quot;];
  
  /*
  Reads App.config:
  
       &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
       &lt;configuration&gt;
       &lt;appSettings&gt;
       &lt;add key=&quot;connectionString&quot; value=&quot;Server=127.0.0.1;Port=0000;Database=some-db;User Id=some-user;Password=some-password;&quot; /&gt;
       &lt;/appSettings&gt;
       &lt;/configuration&gt;
  */
  </code></pre>
  <p><strong>METHOD 2</strong></p>
  <pre><code>// requires: Microsoft.Extensions.Configuration; Microsoft.Extensions.Configuration.Json;
  
  public class DbConfig
  {
       public string ConnectionString { get; set; }
  }
  
  var config = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
  .AddJsonFile(&quot;appsettings.json&quot;).Build();
  var section = config.GetSection(nameof(DbConfig));
  var dbClientConfig = section.Get&lt;DbConfig&gt;();
  var connString = dbClientConfig.ConnectionString;
  
  /*
  Reads appsettings.json:
       {
         &quot;DbConfig&quot;: {    
           &quot;connectionString&quot;: &quot;Server=127.0.0.1;Port=0000;Database=some-db;User Id=some-user;Password=some-password;&quot;
         }
       }
  */
  </code></pre>
  <p>I also used a simpler bare-bones method, that also works in console app but not in the Lambda.</p>
  <p><strong>METHOD 3:</strong></p>
  <pre><code>// requires: Microsoft.Extensions.Configuration;
  IConfiguration _config = new ConfigurationBuilder().AddJsonFile(&quot;appconfig.json&quot;, true, true).Build();
  var _connString = _config[&quot;connectionString&quot;];
  
  /*
  Reads appconfig.json:
       {
         &quot;connectionString&quot;: &quot;Server=127.0.0.1;Port=0000;Database=some-db;User Id=some-user;Password=some-password;&quot;
       }
  */
  </code></pre>
  <p>Again, thanks.</p>