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.
That is two sperate methods that when I try to get a return value, each method returns null.
In a separate .NET Core 3.1 console app, these same methods work perfectly fine.
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?
My situation does not 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.
Any ideas or wisdom on this?
THE CODE:
METHOD 1
// requires: System.Configuration.ConfigurationManager
var connString = System.Configuration.ConfigurationManager.AppSettings["connectionString"];
/*
Reads App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="connectionString" value="Server=127.0.0.1;Port=0000;Database=some-db;User Id=some-user;Password=some-password;" />
</appSettings>
</configuration>
*/
METHOD 2
// 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("appsettings.json").Build();
var section = config.GetSection(nameof(DbConfig));
var dbClientConfig = section.Get<DbConfig>();
var connString = dbClientConfig.ConnectionString;
/*
Reads appsettings.json:
{
"DbConfig": {
"connectionString": "Server=127.0.0.1;Port=0000;Database=some-db;User Id=some-user;Password=some-password;"
}
}
*/
I also used a simpler bare-bones method, that also works in console app but not in the Lambda.
METHOD 3:
// requires: Microsoft.Extensions.Configuration;
IConfiguration _config = new ConfigurationBuilder().AddJsonFile("appconfig.json", true, true).Build();
var _connString = _config["connectionString"];
/*
Reads appconfig.json:
{
"connectionString": "Server=127.0.0.1;Port=0000;Database=some-db;User Id=some-user;Password=some-password;"
}
*/
Again, thanks.