0

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

  • Could you please share the screenshot of your Azure App Settings. – Harshitha Aug 14 '23 at 10:30
  • Add `builder.Configuration.AddEnvironmentVariables();` line in your `Program.cs` file before calling `AddAzureAppConfiguration`. – Harshitha Aug 14 '23 at 10:37
  • The connection string must contain the endpoint (of azure app config). – Harshitha Aug 14 '23 at 11:32
  • I added that (see edits above), no change. I added a screenshot of the Azure App Settings. – WayneRoseberry Aug 14 '23 at 13:22
  • How does your Local `appsettings.json` look? – Harshitha Aug 14 '23 at 13:23
  • The Configuration section in App Service overrides the values which are set in your local. – Harshitha Aug 14 '23 at 13:24
  • Add this line `builder.Configuration.AddEnvironmentVariables();` after retrieving the connection string, then add the connstring to `AddAzureAppConfiguration`. – Harshitha Aug 14 '23 at 13:25
  • { "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" } – WayneRoseberry Aug 14 '23 at 13:31
  • As per you config file, you are missing the Connectionstring => AppConfig value.[Image](https://i.imgur.com/s17akCp.png) – Harshitha Aug 14 '23 at 13:33
  • Instead of actual connection string, add some dummy value in the local appsettings.But the key with AppConfig must be available in the local settings as well. – Harshitha Aug 14 '23 at 13:35
  • How you are deploying your app to Azure? – Harshitha Aug 14 '23 at 13:42
  • I added the AppConfig connection string. If I put in a dummy value, I get an error saying "Invalid connection string format." If I put in the REAL connection string, my website works. This means the Azure App Settings are NOT overriding the appsettings.json like they are supposed to. I am trying to avoid putting the connection string in my appsettings file because that is a stupid thing to check into source control. – WayneRoseberry Aug 14 '23 at 13:48
  • I am deploying to Azure by selecting the project in Visual Studio, right click, "Publish..." – WayneRoseberry Aug 14 '23 at 13:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254910/discussion-between-harshitha-and-wayneroseberry). – Harshitha Aug 14 '23 at 14:19

3 Answers3

0

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
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • The connection string is in the Connection Strings section. I changed it just now to "AppConfig" I get the same error. `Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')` – WayneRoseberry Aug 13 '23 at 21:20
  • What did you select for the "Type" dropdown when you added the connection string? Also, it's worth checking the stack trace to see if you got that error in the same place. Maybe there is a different part of your app that gets this error that uses a different approach for getting a connection string. – developer-partners Aug 13 '23 at 21:27
  • I tried both "SqlServer" and "Custom" as the Type. Neither changed behavior. The exception has always been in the same place, on the line that calls AddAzureAppConfiguration. It is the only place in my code trying to get that connection string. – WayneRoseberry Aug 13 '23 at 21:31
  • `AddAzureAppConfiguration` is not relevant for Web App settings; it's for the Azure App Configuration service, which you don't appear to be using. – Daniel Mann Aug 13 '23 at 21:31
  • As far as I know, the call to AddAzureAppConfiguration connects to Azure Configuration service, so that's not an SqlServer configuration. The Custom type could work, but if that doesn't work, I'd try adding the connection string to the Application Settings section using the ConnectionStrings__AppConfig name. – developer-partners Aug 13 '23 at 21:33
  • The screenshots offered in your examples used SqlServer... so??? – WayneRoseberry Aug 13 '23 at 21:38
  • I tried adding a key to the Application Settings section called "ConnectionString__AppConfig", and that gave the same error. As far as I can tell, the application is failing entirely to read from the Application Settings at all. – WayneRoseberry Aug 13 '23 at 21:42
  • @DanielMann, I am using the Azure App Configuration service, elsewhere. The thing that is the problem is that the call to GetConnectionString() is returning null instead of the value I assigned to the key "AppConfig" – WayneRoseberry Aug 13 '23 at 22:05
  • @WayneRoseberry sorry for the late response. Can you try putting the Azure App Configuration connection string in a different property in your app settings? Maybe Azure doesn't like that you are putting a non database connection string in the ConnectionStrings section. – developer-partners Aug 15 '23 at 17:22
0

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);
Harshitha
  • 3,784
  • 2
  • 4
  • 9
0

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.

Harshitha
  • 3,784
  • 2
  • 4
  • 9