Edit

Share via


Aspire Azure App Configuration integration

Includes: Hosting integration included Hosting integration —&— Client integration included Client integration

Azure App Configuration provides a service to centrally manage application settings and feature flags. Modern programs, especially programs running in a cloud, generally have many components that are distributed in nature. Spreading configuration settings across these components can lead to hard-to-troubleshoot errors during an application deployment. The Aspire Azure App Configuration integration enables you to connect to existing App Configuration instances or create new instances all from your AppHost.

Hosting integration

The Aspire Azure App Configuration hosting integration models the App Configuration resource as the AzureAppConfigurationResource type. To access this type and APIs fro expressing the resource, add the 📦 Aspire.Hosting.Azure.AppConfiguration NuGet package in the AppHost project.

dotnet add package Aspire.Hosting.Azure.AppConfiguration

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Add Azure App Configuration resource

In your AppHost project, call AddAzureAppConfiguration to add and return an Azure App Configuration resource builder.

var builder = DistributedApplication.CreateBuilder(args);

var appConfig = builder.AddAzureAppConfiguration("config");

// After adding all resources, run the app...

builder.Build().Run();

When you add an AzureAppConfigurationResource to the AppHost, it exposes other useful APIs.

Important

When you call AddAzureAppConfiguration, it implicitly calls AddAzureProvisioning(IDistributedApplicationBuilder)—which adds support for generating Azure resources dynamically during app startup. The app must configure the appropriate subscription and location. For more information, see Local provisioning: Configuration.

Provisioning-generated Bicep

If you're new to Bicep, it's a domain-specific language for defining Azure resources. With Aspire, you don't need to write Bicep by-hand, instead the provisioning APIs generate Bicep for you. When you publish your app, the generated Bicep is output alongside the manifest file. When you add an Azure App Configuration resource, the following Bicep is generated:

@description('The location for the resource(s) to be deployed.')
param location string = resourceGroup().location

resource config 'Microsoft.AppConfiguration/configurationStores@2024-06-01' = {
  name: take('config-${uniqueString(resourceGroup().id)}', 50)
  location: location
  properties: {
    disableLocalAuth: true
  }
  sku: {
    name: 'standard'
  }
  tags: {
    'aspire-resource-name': 'config'
  }
}

output appConfigEndpoint string = config.properties.endpoint

output name string = config.name

The preceding Bicep is a module that provisions an Azure App Configuration resource. Additionally, role assignments are created for the Azure resource in a separate module:

@description('The location for the resource(s) to be deployed.')
param location string = resourceGroup().location

param config_outputs_name string

param principalType string

param principalId string

resource config 'Microsoft.AppConfiguration/configurationStores@2024-06-01' existing = {
  name: config_outputs_name
}

resource config_AppConfigurationDataOwner 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
  name: guid(config.id, principalId, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '5ae67dd6-50cb-40e7-96ff-dc2bfa4b606b'))
  properties: {
    principalId: principalId
    roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '5ae67dd6-50cb-40e7-96ff-dc2bfa4b606b')
    principalType: principalType
  }
  scope: config
}

The generated Bicep is a starting point and is influenced by changes to the provisioning infrastructure in C#. Customizations to the Bicep file directly will be overwritten, so make changes through the C# provisioning APIs to ensure they're reflected in the generated files.

Customize provisioning infrastructure

All Aspire Azure resources are subclasses of the AzureProvisioningResource type. This type enables the customization of the generated Bicep by providing a fluent API to configure the Azure resources—using the ConfigureInfrastructure<T>(IResourceBuilder<T>, Action<AzureResourceInfrastructure>) API. For example, you can configure the sku, purge protection, and more. The following example demonstrates how to customize the Azure App Configuration resource:

builder.AddAzureAppConfiguration("config")
    .ConfigureInfrastructure(infra =>
    {
        var appConfigStore = infra.GetProvisionableResources()
                                  .OfType<AppConfigurationStore>()
                                  .Single();

        appConfigStore.SkuName = "Free";
        appConfigStore.EnablePurgeProtection = true;
        appConfigStore.Tags.Add("ExampleKey", "Example value");
    });

The preceding code:

There are many more configuration options available to customize the Azure App Configuration resource. For more information, see Azure.Provisioning.AppConfiguration. For more information, see Azure.Provisioning customization.

Use existing Azure App Configuration resource

You might have an existing Azure App Configuration store that you want to connect to. If you want to use an existing Azure App Configuration store, you can do so by calling the AsExisting method. This method accepts the config store and resource group names as parameters, and uses it to connect to the existing Azure App Configuration store resource.

var builder = DistributedApplication.CreateBuilder(args);

var configName = builder.AddParameter("configName");
var configResourceGroupName = builder.AddParameter("configResourceGroupName");

var appConfig = builder.AddAzureAppConfiguration("config")
                       .AsExisting(configName, configResourceGroupName);

// After adding all resources, run the app...

builder.Build().Run();

For more information, see Use existing Azure resources.

Connect to existing Azure App Configuration store

An alternative approach to using the *AsExisting APIs enables the addition of a connection string instead, where the AppHost uses configuration to resolve the connection information. To add a connection to an existing Azure App Configuration store, call the AddConnectionString method:

var builder = DistributedApplication.CreateBuilder(args);

var config = builder.AddConnectionString("config");

builder.AddProject<Projects.WebApplication>("web")
       .WithReference(config);

// After adding all resources, run the app...

Note

Connection strings are used to represent a wide range of connection information, including database connections, message brokers, endpoint URIs, and other services. In Aspire nomenclature, the term "connection string" is used to represent any kind of connection information.

The connection string is configured in the AppHost's configuration, typically under User Secrets, under the ConnectionStrings section. The AppHost injects this connection string as an environment variable into all dependent resources, for example:

{
    "ConnectionStrings": {
        "config": "https://{store_name}.azconfig.io"
    }
}

The dependent resource can access the injected connection string by calling the GetConnectionString method, and passing the connection name as the parameter, in this case "config". The GetConnectionString API is shorthand for IConfiguration.GetSection("ConnectionStrings")[name].

Add Azure App Configuration emulator resource

Microsoft provide the Azure App Configuration emulator for developers who want a local, lightweight implementation of the Azure App Configuration service to code and test against. In Aspire, you can use this emulator by calling the RunAsEmulator method when you add your resource:

var builder = DistributedApplication.CreateBuilder(args);

var appConfig = builder.AddAzureAppConfiguration("config")
                       .RunAsEmulator();

// After adding all resources, run the app...

builder.Build().Run();

The Azure App Configuration emulator isn't installed on your local computer. Instead, it's accessible to Aspire as a container. The RunAsEmulator method creates and starts the container when the AppHost starts using the azure-app-configuration/app-configuration-emulator image. For more information, see Container resource lifecycle.

Configure Azure App Configuration emulator container

There are various configurations available to container resources. For example, you can configure the container's port, environment variables, it's lifetime, and more.

Configure Azure App Configuration emulator host port

By default, Aspire assigns a random host port for the emulator container. If you want to use a specific port, chain calls on the container resource builder provided by the RunAsEmulator method as shown in the following example:

var builder = DistributedApplication.CreateBuilder(args);

var appConfig = builder.AddAzureAppConfiguration("config")
                     .RunAsEmulator(
                         emulator =>
                         {
                             emulator.WithHostPort(28000);
                         });

// After adding all resources, run the app...

The preceding code configures the emulator container's endpoint to listen on ports 28000.

Configure Azure App Configuration emulator with persistent lifetime

To configure the emulator container with a persistent lifetime, call the WithLifetime method on the emulator container resource and pass ContainerLifetime.Persistent:

var builder = DistributedApplication.CreateBuilder(args);

var appConfig = builder.AddAzureAppConfiguration("config")
                     .RunAsEmulator(
                         emulator =>
                         {
                             emulator.WithLifetime(ContainerLifetime.Persistent);
                         });

// After adding all resources, run the app...

For more information, see Container resource lifetime.

Configure Azure App Configuration emulator with data volume

To add a data volume to the Azure App Configuration emulator resource, call the WithDataVolume method on the emulator resource:

var builder = DistributedApplication.CreateBuilder(args);

var appConfig = builder.AddAzureAppConfiguration("config")
                       .RunAsEmulator(
                           emulator =>
                           {
                               emulator.WithDataVolume();
                           });

// After adding all resources, run the app...

The data volume is used to persist the emulator data outside the lifecycle of its container. The data volume is mounted at the /data path in the emulator container and when a name parameter isn't provided, the name is autogenerated from the application and resource names. For more information on data volumes and details on why they're preferred over bind mounts, see Docker docs: Volumes.

Configure Azure App Configuration emulator with data bind mount

To add a data bind mount to the Azure App Configuration emulator resource, call the WithDataBindMount method:

var builder = DistributedApplication.CreateBuilder(args);

var appConfig = builder.AddAzureAppConfiguration("config")
                       .RunAsEmulator(
                           emulator =>
                           {
                               emulator.WithDataBindMount("../Emulator/Data");
                           });

// After adding all resources, run the app...

Important

Data bind mounts have limited functionality compared to volumes, which offer better performance, portability, and security, making them more suitable for production environments. However, bind mounts allow direct access and modification of files on the host system, ideal for development and testing where real-time changes are needed.

Data bind mounts rely on the host machine's filesystem to persist the emulator data across container restarts. The data bind mount is mounted at the ../Emulator/Data path on the host machine relative to the AppHost directory (IDistributedApplicationBuilder.AppHostDirectory) in the emulator container. For more information on data bind mounts, see Docker docs: Bind mounts.

Client integration

To get started with the Aspire Azure App Configuration client integration, install the 📦 Aspire.Microsoft.Extensions.Configuration.AzureAppConfiguration NuGet package in the client-consuming project, that is, the project for the application that uses the App Configuration client. The App Configuration client integration registers a CosmosClient instance that you can use to interact with App Configuration.

dotnet add package Aspire.Microsoft.Extensions.Configuration.AzureAppConfiguration

In the Program.cs file of your client-consuming project, call the AddAzureAppConfiguration extension method on any IHostApplicationBuilder to register the required services to flow Azure App Configuration values into the IConfiguration instance for use via the dependency injection container. The method takes a connection name parameter.

builder.AddAzureAppConfiguration(connectionName: "config");

Tip

The connectionName parameter must match the name used when adding the App Configuration resource in the AppHost project. In other words, when you call AddAzureAppConfiguration in the AppHost and provide a name of config that same name should be used when calling AddAzureAppConfiguration in the client-consuming project. For more information, see Add Azure App Configuration resource.

You can then retrieve the IConfiguration instance using dependency injection. For example, to retrieve the client from an example service:

public class ExampleService(IConfiguration configuration)
{
    private readonly string _someValue = configuration["SomeKey"];
}

Use feature flags

To use feature flags, install the 📦 Microsoft.FeatureManagement NuGet package:

dotnet add package Microsoft.FeatureManagement

App Configuration doesn't load feature flags by default. To load feature flags, you pass the Action<AzureAppConfigurationOptions> configureOptions delegate when calling builder.AddAzureAppConfiguration.

builder.AddAzureAppConfiguration(
    "config",
    configureOptions: options => options.UseFeatureFlags());

// Register feature management services
builder.Services.AddFeatureManagement();

You can then use IFeatureManager to evaluate feature flags in your app. Consider the following example ASP.NET Core Minimal API app:

using Microsoft.Extensions.Hosting;
using Microsoft.FeatureManagement;

var builder = WebApplication.CreateBuilder(args);

builder.AddAzureAppConfiguration(
    "config",
    configureOptions: options => options.UseFeatureFlags());

// Register feature management services
builder.Services.AddFeatureManagement();

var app = builder.Build();

app.MapGet("/", async (IFeatureManager featureManager) =>
{
    if (await featureManager.IsEnabledAsync("NewFeature"))
    {
        return Results.Ok("New feature is enabled!");
    }

    return Results.Ok("Using standard implementation.");
});

app.Run();

For more information, see .NET Feature Management.

Configuration

The Aspire Azure App Configuration library provides multiple options to configure the Azure App Configuration connection based on the requirements and conventions of your project. The App Config endpoint is required to be supplied, either in AzureAppConfigurationSettings.Endpoint or using a connection string.

Use a connection string

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddAzureAppConfiguration():

builder.AddAzureAppConfiguration("config");

Then the App Configuration endpoint is retrieved from the ConnectionStrings configuration section. The App Configuration store URI works with the AzureAppConfigurationSettings.Credential property to establish a connection. If no credential is configured, the DefaultAzureCredential is used.

{
  "ConnectionStrings": {
    "config": "https://{store_name}.azconfig.io"
  }
}

Use configuration providers

The Aspire Azure App Configuration library supports Microsoft.Extensions.Configuration. It loads the AzureAppConfigurationSettings from configuration by using the Aspire:Microsoft:Extensions:Configuration:AzureAppConfiguration key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Microsoft": {
      "Extensions": {
        "Configuration": {
          "AzureAppConfiguration": {
            "Endpoint": "YOUR_APPCONFIGURATION_ENDPOINT_URI"
          }
        }
      }
    }
  }
}

For the complete App Configuration client integration JSON schema, see ./ConfigurationSchema.json.

Use inline delegates

You can also pass the Action<AzureAppConfigurationSettings> configureSettings delegate to set up some or all the options inline, for example to set App Configuration endpoint from code:

builder.AddAzureAppConfiguration(
    "config",
    configureSettings: settings => settings.Endpoint = "http://YOUR_URI");

Observability and telemetry

Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about integration observability and telemetry, see Aspire integrations overview. Depending on the backing service, some integrations may only support some of these features. For example, some integrations support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

Logging

The Aspire Azure App Configuration integration uses the following log categories:

  • Microsoft.Extensions.Configuration.AzureAppConfiguration.Refresh

Tracing

The Aspire Azure App Configuration integration doesn't make use any activity sources thus no tracing is available.

Metrics

The Aspire Azure App Configuration integration currently doesn't support metrics.

See also