0

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
  • If anybody wants to answer it is academic at this point. I gave up on using Enviornment.GetEnvironmentVariable and instead am using dependency injection on my controllers to get the IConfiguration object. That gets all the app settings. This annoying because what I was trying to do was store the connection string so I could use the ApplicationConfiguration service and retrieve secrets from there. The fact I had this working and suddenly it stopped is troubling to me. – WayneRoseberry Aug 23 '23 at 02:23
  • Your screenshot seems to be the older version of Azure App Service.You can see few updates/changes in the UI of App Service. – Harshitha Aug 28 '23 at 08:38
  • Options to add App Settings/Connection Strings has been moved from Configuration Section. There is a new option to add Environment Variables under settings.[Image](https://i.stack.imgur.com/s2vRm.png) – Harshitha Aug 28 '23 at 08:41
  • I have added the key-value pair under [Connection Strings](https://i.stack.imgur.com/cUPO4.png).You can see the value is available under [Environment Variables](https://i.stack.imgur.com/pIw2p.png) – Harshitha Aug 28 '23 at 08:50
  • You need to update the Azure Portal to get the latest version.If possible, try to create a new Azure App Service and deploy the app to the new AppService. – Harshitha Aug 28 '23 at 08:53

1 Answers1

0

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

Harshitha
  • 3,784
  • 2
  • 4
  • 9