0

I have added key value pair in Azure app Configuration and trying to read it in Startup.cs class file. Please suggest how to do that.

public class Startup : FunctionsStartup
{
    private static readonly string url= "*******************";
   
    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        string connectionString=????? //How to get this value from Azure app config
        builder.Services.AddDbContext<DbContext, Context>(
            options => SqlServerDbContextOptionsExtensions.UseSqlServer(options, connectionString));
       builder.ConfigurationBuilder.AddAzureAppConfiguration(url);
    }
}
uvr
  • 515
  • 4
  • 12
Arshit Singh
  • 103
  • 4
  • 14

3 Answers3

12

You need to split out configuration of your configuration from the registration of your application services. In other words, setup of Azure App Configuration should be done in ConfigureAppConfiguration while the registration of your DbContext should be done from the Configure method. This will allow you to access the Configuration property of the FunctionsHostBuilderContext in the Configure method to retrieve your connection string:

public class Startup : FunctionsStartup
{
    private static readonly string url= "*******************";
   
    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        // register AzureAppConfiguration only
        builder.ConfigurationBuilder.AddAzureAppConfiguration(url);
    }

    public override void Configure(IFunctionsHostBuilder builder)
    {
        var context = build.GetContext();
        var config = context.Configuration;

        string connectionString = config["AzureAppConfigKeyName"];
        builder.Services.AddDbContext<DbContext, Context>(
            options => SqlServerDbContextOptionsExtensions.UseSqlServer(options, connectionString)
        );
    }
}

The value you pass to the indexer of the config object is going to depend on what you named your key in Azure App Configuration. Assuming you called it "DatabaseConnectionString" then that is exactly what you'll pass, ie: config["DatabaseConnectionString"].

Prefixes

Azure App Configuration has a few features that can control how the key names are handled by the application, specifically the ability to "Trim Prefixes". You aren't using that here (since you are just passing the URL) but suppose you had keys in Azure App Configuration such as MyFunction:DatabaseConnectionString. By default you would access this using it's full name:

var cs1 = config["MyFunction:DatabaseConnectionString"];
// or
var cs2 = config.GetSection("MyFunction")["DatabaseConnectionString"];

You could however specify the long-form connection options. This would allow you to only select keys that begin with that prefix and optionally trim them off:

builder.ConfigurationBuilder.AddAzureAppConfiguration(s => 
{
    s.Connect(url, new DefaultAzureCredential());
    s.Select("MyFunction:*");
    s.TrimKeyPrefix("MyFunction:");
});

Which would now make your connection string available with a shorter key:

string connectionString = config["DatabaseConnectionString"];

This feature is especially useful if you have a lot of different settings in your Azure App Configuration instance and only want to pull in those specifically related to your application (labels can also be used for this purpose).

Azure App Config Connection - Environment Variables

Finally, a suggestion. Don't store the url or connection details of your Azure App Configuration instance hard-coded in your application. Make use of environment variables for this. "Application Settings" in Azure App Service/Azure Functions are automatically added as environment variables to your application.

Before ConfigureAppConfiguration has run, a default set of configuration sources has already been added to the context/builder. These include host.json, appsettings.json, local.settings.json (when running locally) and environment variables. A "better" way to configure your link to Azure App Configuration is to use this mechanism.

For example, you can add a FunctionApp Application Setting named AzureAppConfigUrl that contains this value:

Application Settings

Locally you'd add a corresponding entry to your local.settings.json file:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureAppConfigUrl": "https://your-configuration.azconfig.io"
  }
}

You'd then reference this value when configuring Azure App Configuration. Locally it will pull from the JSON file and when running in Azure it will read from the Application Settings (or rather, the environment variables):

public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
   var context = build.GetContext();
   var url = context.Configuration["AzureAppConfigUrl"];
   builder.ConfigurationBuilder.AddAzureAppConfiguration(url);
}
pinkfloydx33
  • 11,863
  • 3
  • 46
  • 63
0

ConfigureAppConfiguration method should be used exclusively to configure application's IConfiguration object. There is another dedicated method Configure which should be used to configure application itself (e.g. dependency injection).

Here is the simple example:

public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
    FunctionsHostBuilderContext context = builder.GetContext();

    builder.ConfigurationBuilder
        .SetBasePath(context.ApplicationRootPath)
        .AddJsonFile("appsettings.json")
        .AddEnvironmentVariables();
}

public override void Configure(IFunctionsHostBuilder builder)
{
    IConfiguration configuration = builder.Services.BuildServiceProvider().GetRequiredService<IConfiguration>();

    // use configuration object 
}

If that doesn't work for you for some reason, you can always fallback to get the value from environmental variables, since settings are added to Environment

Environment.GetEnvironmentVariable("DatabaseConnectionString"))
Darjan Bogdan
  • 3,780
  • 1
  • 22
  • 31
  • I am trying to read values from azure app configuration, not from appsetting.json file – Arshit Singh Apr 28 '21 at 07:06
  • @ArshitSingh you can still load configuration data from Azure App configuration. This is just example of configuration from appsettings.json, the focus should be on `Configure` method and its usage. – Darjan Bogdan Apr 28 '21 at 07:33
  • I am able to read it another class with help of IConfiguration but how to do it in this same class ConfigureAppConfiguration method – Arshit Singh Apr 28 '21 at 08:38
  • you shouldn't do it there, configure Dependency Injection in `Configure` method – Darjan Bogdan Apr 28 '21 at 08:41
0

The doc below walks you through how to use Azure App Configuration in Azure Functions https://learn.microsoft.com/azure/azure-app-configuration/quickstart-azure-functions-csharp

Zhenlan Wang
  • 1,213
  • 8
  • 10