Azure Connection String Env Variable not populating

Toren Robinson 0 Reputation points
2024-09-13T16:07:49.6166667+00:00

I have a web app on Azure that I am trying to connect to a mysql db hosted on aiven.io. The connection works if I hard code the connection string in appsettings.json. When I enter the exact same connection string in Azure Env Variables the connection doesn't work.

Here is my env variable setup

User's image

User's image

Here is my Program.cs connection string setup

User's image

As stated, when I hard code the connection string in appsettings.json and deploy, the connection works on azure web app. The azure env variable doesn't seem to be picking up at runtime.

Any clues of why?

Developer technologies ASP.NET ASP.NET Core
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2024-09-13T16:20:02.66+00:00

    That isn't what the Connection Strings section is for. Since you are running an ASP.NET Core app then you need to set your connection string information under App Settings. This section overrides whatever is specified in the apps appsettings.*.json file. This is where you would override the default connection string with whatever connection string you need to use for your app. At runtime everything in this section are bubbled up as env variables to the app process.

    The Connection Strings section is designed for non-.NET apps and NET Framework apps that use a web.config that you need to override. You can read more about when you'd use this approach here.

    Move your connection string into App Settings and restart your app and everything should work. Assuming you're following the standard connection string approach in appsettings.

    {
       "ConnectionStrings": {
          "SomeConnection" : "..."
       }
    }
    

    Then the setting in Azure would look something like ConnectionStrings__SomeConnection.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-09-13T17:35:24.45+00:00

    the env setting support adds the prefix. try:

    var freeMySqlString = builder.Configuration.GetConnectionString("freeMySql");

    0 comments No comments

  3. Toren Robinson 0 Reputation points
    2024-09-13T18:00:15.6433333+00:00

    Also, here is my launchsettings.json file, is there anything here that could be stopping the azure env variables from being picked up:

    {
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:52478",
          "sslPort": 44385
        }
      },
      "profiles": {
        "http": {
          "commandName": "Project",
          "dotnetRunMessages": true,
          "launchBrowser": true,
          "launchUrl": "swagger",
          "applicationUrl": "http://localhost:5217",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "https": {
          "commandName": "Project",
          "dotnetRunMessages": true,
          "launchBrowser": true,
          "launchUrl": "swagger",
          "applicationUrl": "https://localhost:7056;http://localhost:5217",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "launchUrl": "swagger",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.