Reputation: 93
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
Upvotes: 1
Views: 692
Reputation: 7392
Even if you run the code locally you get the same error.
This is because while publishing the App, the application will build and load the code locally.
And the code which you have provided does not satisfy any of the condition as you are not setting the Connection String
in either appsettings.json
or in the Environment Variable
(Which I understood this from your previous question).
I have provided 2 workarounds , check and follow any one.
Way 1 :
Way 2:
appsettings.json
file."ConnectionStrings": {
"AppConfig": "Sample"
}
Now publish the App.
Navigate to the KUDU Console (site\wwwroot
), you can see the appsettings.json
file.
As you don't want to set any values in your Configuration file.
delete the Connection String Section from the appsettings.json`.
Now save and run the deployed URL.
To confirm whether we have value for CUSTOMCONNSTR_AppConfig
in GetEnvironmentVariable
, write the below line of code in Index.cshtml
before publishing the App.
@Environment.GetEnvironmentVariable("CUSTOMCONNSTR_AppConfig")
You can see Iam able to retrieve the value using Environment.GetEnvironmentVariable("CUSTOMCONNSTR_AppConfig")
Env Variables:
Output:
Upvotes: 0