fbpx

Just as all Azure Web Apps need configuration values, most applications also need to have database Connection String values configured. With Azure Web Apps the Connection Strings are stored/retrieved in a very similar fashion as Azure Web App Application Settings. Connection Strings are also Key / Value pairs of String values, but are separated out into their own section.

Connection Strings are typically used to store the connection information for one or more databases the Web App needs to connect to for storing and retrieving data. The Connection String types supported are SQL Database, SQL Server, MySQL, PostgreSQL, and Custom. Most often the Connection Strings used will be for some kind of SQL RDMS, but the Custom type allows for an additional Connection String to be configured any other type of database connection necessary (such as Cosmos DB and Azure Storage).

As with Application Settings, the Connection Strings are accessed as normal from .NET code and the values will come from what is set within the Azure Portal. In other development environments (Node.js, Java, PHP, Python) the Connection Strings are exposed to code as Environment Variables. Additionally, the Connection Strings are editable within the Azure Portal, but are read-only when access through code.

Connection String within the Azure Portal

Managing the Connection Strings for an Azure Web App is performed through the Azure Portal.

Here are the necessary steps for locating and accessing the Connection Strings for an Azure Web App within the portal:

  1. Open the Azure Portal via https://portal.azure.com
  2. Navigate to the Azure App Service Web App within the portal.
  3. Under Settings open up the Configuration option
  4. The Connection strings section can be used to manage the settings for the application.
Azure Web App: Connection Strings 1
Screenshot: Azure Web App Connection Strings

Add / Edit Connection Strings

When creating and/or editing Connection Strings (such as through Configuration -> Connection strings in the Azure Portal) the connection strings consist of a Key / Value pair for the name and value of the connection string. However, they also include another Type value set on the Connection String that defines what type of connection string this is. This helps specify if the connection string is used for a Azure SQL Database, SQL Server, or other types of databases and services.

There are several different Type options available for the Connection Strings:

  • MySQL – MySQL server
  • SQLServer – Microsoft SQL Server (such as that running in a VM)
  • SQLAzure – Azure SQL Database
  • PostgreSQL – PostgreSQL server
  • Custom – a general type used for any other database / service connection string (such as Azure Storage, Cosmos DB, or others)
Azure Web App: Connection Strings 2
Screenshot: Add / Edit Connection String pane

Access Connection Strings using ASP.NET Core (C#)

Using C# code in .NET Core to retrieve the Connections Strings for the Azure Web App is done using the standard ASP.NET Core dependency injection pattern in the following code example:

using Microsoft.Extensions.Configuration;

namespace MyNamespace 
{
    public class MyClass
    {
        private IConfiguration _configuration;
    
        public SomeClass(IConfiguration configuration)
        {
            _configuration = configuration;
        }
    
        public SomeMethod()
        {
            // retrieve App Service connection string
            var myConnString = _configuration.GetConnectionString("MyDbConnection");
        }
    }
}

Access Connection Strings using .NET Framework (C#)

Retrieving the Connection Strings for the Azure Web App is performed from .NET using the ConfigurationManager class in a very similar fashion as how Application Settings are retrieved.

Here’s an example of retrieving a Connection String from C# code:

using System.Configuration;

var key = "MyDBConnString";
string value = ConfigurationManager.ConnectionStrings[key]
    .ConnectionString;

Access Connection Strings from Node.js

In Node.js the Azure Web App Connection Strings are exposed as Environment Variables. Accessing the Connection Strings through the Environment Variables is exactly like accessing Application Settings, but with a different string prefixing the Connection String key name.

Here are the Connection String Environment Variable key name prefixes:

  • SQL Database prefix is SQLAZURECONNSTR_
  • SQL Server prefix is SQLCONNSTR_
  • MySQL prefix is MYSQLCONNSTR_
  • Custom prefix is CUSTOMCONNSTR_

Here’s an example of retrieving a Connection String from Javascript running on Node.js:

var value = process.env.SQLCONNSTR_MyDBConnString;

Access Connection Strings from Java

Azure Web App Connection Strings are exposed to Java code as Environment Variables. These are accessed in the same method as Application Settings except with different string prefixes on the Connection String key names.

Here are the Connection String Environment Variable key name prefixes:

  • SQL Database prefix is SQLAZURECONNSTR_
  • SQL Server prefix is SQLCONNSTR_
  • MySQL prefix is MYSQLCONNSTR_
  • Custom prefix is CUSTOMCONNSTR_

Here’s an example of retrieving a Connection String from Java code:

String value = System.getenv("SQLCONNSTR_MyDBConnString");

Happy coding!

Microsoft MVP

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading