asp.netazureconnection-stringazure-application-settings

ConnectionStrings from Azure Application Settings is returning null string on


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". enter image description here


Solution

  • 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

    1. If you add the connection strings in the Application Settings section, the format should be the following TopObjectNameInJson__NestedPropertyName. In your case, it would be ConnectionStrings__AppConfig. Azure uses double underscores, __, for indicating nested properties in appsettings.json file. Screenshot showing how you can add your connection strings in the Application Settings section
    2. If you add the connection strings in the Connection Strings section, you can just use the connection string property name to override. In your case, it would be just AppConfig. Screenshot showing how you can add your connection strings in the Connection Strings section