Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Includes:
Hosting integration —&—
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 .NET Aspire Azure App Configuration integration enables you to connect to existing App Configuration instances or create new instances all from your app host.
Hosting integration
The .NET 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 app host 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 app host 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 app host, 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 .NET 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-05-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-05-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 .NET 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:
- Chains a call to the ConfigureInfrastructure API:
- The
infra
parameter is an instance of the AzureResourceInfrastructure type. - The provisionable resources are retrieved by calling the GetProvisionableResources() method.
- The single AppConfigurationStore is retrieved.
- The AppConfigurationStore.SkuName is assigned to
Free
. - A tag is added to the App Configuration store with a key of
ExampleKey
and a value ofExample value
.
- The
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 an existing Azure App Configuration store
An alternative approach to using the *AsExisting
APIs enables the addition of a connection string instead, where the app host 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 .NET Aspire nomenclature, the term "connection string" is used to represent any kind of connection information.
The connection string is configured in the app host's configuration, typically under User Secrets, under the ConnectionStrings
section. The app host 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]
.
Client integration
To get started with the .NET 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 app host project. In other words, when you call AddAzureAppConfiguration
in the app host 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 .NET 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 .NET 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
.NET 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 .NET 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 .NET Aspire Azure App Configuration integration uses the following log categories:
Microsoft.Extensions.Configuration.AzureAppConfiguration.Refresh
Tracing
The .NET Aspire Azure App Configuration integration doesn't make use any activity sources thus no tracing is available.
Metrics
The .NET Aspire Azure App Configuration integration currently doesn't support metrics.
See also
.NET Aspire