I have a connection string I entered into the App Settings for my application. This setting was retrieving as expected up until today. Now, when I try to publish my application, the environment variable is not in the list of environment variables available from Environment.GetEnvironmentVariables. When I attempt to retrieve it from either builder.Configuration.ConnectionStrings() or Environment.GetEnvironmentVariable, I get a null string.
What would cause an environment variable to not get set? Everything seems as if it is configured properly.
Below is a screen shot from the App Settings, Configuration page show that the variable is set there.
Below is a screen shot from KUDU, showing that the environment variable CUSTOMCONNSTR_AppConfig does exist on the app service:
Below is the code that attempts to retrieve the connection string. I added the exception throw on string empty or null check to avoid any confusion over where the error is coming from:
static void ConfigurationEnvironmentVariablesNecessaryToInitializeServices(WebApplicationBuilder builder)
{
// this section tries to get the connection string, which works locally from local secret store, but for some reason it was
// not getting set via Azure App Settings. Thus the fallback to get the resulting environment variable which does seem to
// be working in Azure.
string issueIdentityAPIConfigConnectionString = builder.Configuration.GetConnectionString("AppConfig");
builder.Configuration.AddEnvironmentVariables();
if (string.IsNullOrEmpty(issueIdentityAPIConfigConnectionString))
{
issueIdentityAPIConfigConnectionString = Environment.GetEnvironmentVariable("CUSTOMCONNSTR_AppConfig"); ;
}
if (string.IsNullOrEmpty(issueIdentityAPIConfigConnectionString))
{
throw new Exception("Connection string was null in both builder.Configuration.GetConnectionString(\"AppConfig\") and Environment.GetEnvironmentVariable(\"CUSTOMCONNSTR_AppConfig\")");
}
Below is the exception being thrown when I publish the app:
Unhandled exception. System.Exception: Connection string was null in both builder.Configuration.GetConnectionString("AppConfig") and Environment.GetEnvironmentVariable("CUSTOMCONNSTR_AppConfig")
at Program.<<Main>$>g__ConfigurationEnvironmentVariablesNecessaryToInitializeServices|0_2(WebApplicationBuilder builder) in C:\Users\wayne\source\repos\IssueIdentity\IssueIdentityAPI\Program.cs:line 72
at Program.<Main>$(String[] args) in C:\Users\wayne\source\repos\IssueIdentity\IssueIdentityAPI\Program.cs:line 14