WayneRoseberry
WayneRoseberry

Reputation: 93

Connection string not available in azure app via environment

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. enter image description here

Below is a screen shot from KUDU, showing that the environment variable CUSTOMCONNSTR_AppConfig does exist on the app service: enter image description here

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

Answers (1)

Harshitha
Harshitha

Reputation: 7392

Even if you run the code locally you get the same error.

enter image description here

  • 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 :

  • As we are facing issue with the given code while publishing from Visual Studio.
  • Open the Project Folder in VSCode and deploy the code from there.

enter image description here

  • App will be published even if you have errors in the Code(as we are unable to run locally, better to choose this option).

enter image description here

Way 2:

  • Add the sample/dummy connection string in 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:

enter image description here

Output:

enter image description here

Upvotes: 0

Related Questions