anthony_s
anthony_s

Reputation: 137

AWS .NET Core 3.1 Mock Lambda Test Tool, cannot read AppSettings.json or App.config

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.

Upvotes: 2

Views: 5235

Answers (1)

osynavets
osynavets

Reputation: 1520

From this blog post

The test tool is an ASP.NET Core application that loads and executes the Lambda code.

Which means that this web app has its own config file that is different from your application's one. And if you put a breakpoint in startup class, you will see that configuration has different fields kept in rather that those in your appsettings.json or whatever file.

solution #1

Register current directory as a folder where to search config files

new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: true)

Upvotes: 9

Related Questions