I am trying to retrieve a connection string stored in Application Settings, Connection strings in Azure for my web application. The application works fine run locally, but when I publish to Azure the call to AddAzureAppConfiguration throws saying that the connection string is null.
The string coming back from the call to builder.Configuration.GetConnectionString() is null.
using IssueIdentityAPI;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Configuration.AddEnvironmentVariables();
string issueIdentityAPIConfigConnectionString = builder.Configuration.GetConnectionString("AppConfig");
// Load configuration from Azure App Configuration
builder.Configuration.AddAzureAppConfiguration(issueIdentityAPIConfigConnectionString);
The name of the connection string in the Azure Portal is "ConnectionStrings:AppConfig". I had previously named it just "AppConfig", but that yields the same behavior. I have the screenshot pasted below, showing that the key name of the connection string is "AppConfig".
2
Answers
Still do not know why GetConnectionString is returning null, but Harshita pointed in discussion out the variable ought to be visible in Kudu, and I found it there. I switched to just accessing it via the environment as per the following code:
// 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"); } // Load configuration from Azure App Configuration builder.Configuration.AddAzureAppConfiguration(issueIdentityAPIConfigConnectionString);
In Azure Portal, there are 2 places where you can add your connection strings: Application Settings and Connection Strings. You can add the connection strings in either one of those sections:
Screenshot showing where to add connection strings in Azure portal configurations
Screenshot showing how you can add your connection strings in the Application Settings section
Screenshot showing how you can add your connection strings in the Connection Strings section