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".
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:
TopObjectNameInJson__NestedPropertyName
. In your case, it would be ConnectionStrings__AppConfig
. Azure uses double underscores, __
, for indicating nested properties in appsettings.json file.