ConnectionStrings from Azure Application Settings is returning null string on

544 Views Asked by At

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

3

There are 3 best solutions below

9
developer-partners On BEST ANSWER

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
0
WayneRoseberry On

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);
0
Harshitha On

If you want to override any of the appsettings in the Configuration Section, then the value must be available in the appsettings.json file of the deployed app.

The key name must be same as in the appsettings.json file to override the value.

As you don`t want to share the key-values in the configuration file, you can even add the variables in the Environment Variables section in Visual Studio.

enter image description here

As you are sharing the code in GitHub repo it is clear that even you don't want to go for that option as well.

Add the key-value inApp Service => Environment Variable => Connection Strings section.

enter image description here

When we add Application settings in the Configuration section of the deployed app, the values will be available as Environment Variables in the KUDU Console.

If you select type as SQLServer, then the variable will be available as SQLCONNSTR_AppConfig.

enter image description here For Custom, the variable will be available as CUSTOMCONNSTR_AppConfig.

enter image description here

Use the Variable to retrieve the value based on the type.