Retrieving Web Site Configuration Settings from Azure for use in a Console App

134 Views Asked by At

I'm trying to get the settings and connection strings from an Azure web site for use in an internal console tool. This is proving much more difficult than I would have expected. I'm tempted to start looking into bypassing the C# SDK altogether and just use an HttpClient, but I'm not quite there yet.

This is what I have so far:

var armClient = new ArmClient(new DefaultAzureCredential(), "[My subscription ID]");
var webApp = armClient.GetWebSiteResource(new Azure.Core.ResourceIdentifier("[my resource identifier]"));
var b = webApp.GetAllConfigurationData();
var c = b.First();
Console.WriteLine(c.Name);
Console.WriteLine(c.ConnectionStrings == null ? "null" : c.ConnectionStrings.Count);
Console.WriteLine(c.AppSettings == null ? "null" : c.AppSettings.Count);

What I'm getting is the following:

[the correct name of the website]
null
null

Settings and connection strings definitely exist; I can see them on the config page for the website in Azure Portal.

The (extremely minimal and somewhat placeholderish) documentation for GetAllConfigurationData suggests that multiple calls and iterations may be needed, but I'm not even sure what to do with that.

Anyone have any ideas here? Am I going about this all wrong and there's a better way? The console app is non-negotiable, unfortunately.


EDIT: Adding a screenshot of where in the portal the config settings I'm looking at are. (Highlighted one at the bottom.)

Location in Portal Nav

Also, if it helps, I'm looking for the programmatic equivalent of this Powershell command:

az webapp config appsettings list --name foo --resource-group bar

2

There are 2 best solutions below

3
Aariyanflix On

To retrieve configuration settings from Azure for use in a console application, you can utilize Azure App Configuration service. Azure App Configuration is a service that enables you to centrally manage application settings and feature flags. Here's a general outline of how you can achieve this:

  1. Create an Azure App Configuration Store: First, you need to create an Azure App Configuration store in your Azure subscription. You can do this through the Azure portal.

  2. Add Configuration Settings: Add the configuration settings (key-value pairs) that you want to retrieve in your console application to the Azure App Configuration store.

  3. Install the Azure App Configuration Client Library: Install the Azure App Configuration client library for .NET. You can do this using NuGet package manager. You will need to install the Microsoft.Extensions.Configuration.AzureAppConfiguration package.

  4. Configure Access to Azure App Configuration: Configure access to the Azure App Configuration store in your console application. This typically involves providing connection settings such as the connection string or other authentication methods.

  5. Retrieve Configuration Settings: Use the Azure App Configuration client library in your console application to retrieve the configuration settings from the Azure App Configuration store.

Here's a simplified code example:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
using System;

class Program
{
    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder();
        
        // Add Azure App Configuration as a configuration source
        builder.AddAzureAppConfiguration(options =>
        {
            options.Connect("<your-connection-string>")
                   .ConfigureKeyVault(kv =>
                   {
                       kv.SetCredential(new DefaultAzureCredential());
                   });
        });

        IConfigurationRoot configuration = builder.Build();

        // Retrieve configuration settings
        string setting1 = configuration["Setting1"];
        string setting2 = configuration["Setting2"];

        Console.WriteLine($"Setting1: {setting1}");
        Console.WriteLine($"Setting2: {setting2}");
    }
}

Replace <your-connection-string> with the connection string of your Azure App Configuration store.

In this example, Setting1 and Setting2 are the keys of the configuration settings you want to retrieve. You can add more settings as needed. Once retrieved, you can use these settings in your console application as required.

0
vdL On

I get a similar non result with "GetAllConfigurationDataAsync" but the code below, using other methods of the WebsiteResource class, works for me:

var armClient = host.Services.GetRequiredService<ArmClient>();
var resource = new ResourceIdentifier(resourceId);
var website = armClient.GetWebSiteResource(resource);
var connectionStrings = await website.GetConnectionStringsAsync();
foreach (var prop in connectionStrings.Value.Properties)
{
    Console.WriteLine($"{prop.Key}:{prop.Value.Value}");
}
var applicationSettings = await website.GetApplicationSettingsAsync();
foreach (var appSetting in applicationSettings.Value.Properties)
{
    Console.WriteLine($"{appSetting.Key}:{appSetting.Value}");
}