I’m trying add support for appsettings.json in a Lambda project template. I know that we could retrieve application settings from environment variables or AWS Secrets Manager but, the current use case requires the configuration values to be ready from appsettings.json only.
Below is the code:-
//Declaration
private readonly IConfiguration _configuration;
//Constructor
_configuration = GetConfiguration();
//Configuration
private IConfiguration GetConfiguration()
{
var assemblyLoc = Assembly.GetExecutingAssembly().Location;
var directoryPath = Path.GetDirectoryName(assemblyLoc);
var configFilePath = Path.Combine(directoryPath!, "config/appsettings.json");
var cfgBuilder = new ConfigurationBuilder().AddJsonFile(configFilePath, optional: false, reloadOnChange: true);
cfgBuilder.AddEnvironmentVariables();
var config = cfgBuilder.Build();
return config;
}
The moment I try to do this,
_configuration.GetConnectionString(“SAMPLE_CONNECTION_STRING”)
I’m getting this error:-
System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
I should be able to configure appsettings.json for the Lambda project template.
user25193170 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.