콘텐츠로 이동

Azure App Configuration integration

이 콘텐츠는 아직 번역되지 않았습니다.

Azure App Configuration logo

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.

The Aspire Azure App Configuration hosting integration models the App Configuration resource as the AzureAppConfigurationResource type. To access this type and APIs for expressing them within your AppHost project, install the 📦 Aspire.Hosting.Azure.AppConfiguration NuGet package:

Aspire CLI — Aspire.Hosting.Azure.AppConfiguration 패키지 추가
aspire add azure-appconfiguration

Aspire CLI는 대화형입니다. 프롬프트 시 알맞은 검색 결과 선택:

Aspire CLI — 출력 예시
Select an integration to add:
> azure-appconfiguration (Aspire.Hosting.Azure.AppConfiguration)
> Other results listed as selectable options...

To add an Azure App Configuration resource to your AppHost project, call the AddAzureAppConfiguration method providing a name:

C# — AppHost.cs
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.

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:

:::code language=“bicep” source=”../snippets/azure/AppHost/config/config.bicep”:::

Generated Bicep — config.bicep
@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:

Generated Bicep — config-roles.bicep
@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.

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 API. For example, you can configure the sku, purge protection, and more. The following example demonstrates how to customize the Azure App Configuration resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
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 of Example value.

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

Use existing Azure App Configuration resource

Section titled “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 instance

Section titled “Connect to an existing Azure App Configuration instance”

You might have an existing Azure App Configuration service that you want to connect to. You can chain a call to annotate that your AzureAppConfigurationResource is an existing resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var existingAppConfigName = builder.AddParameter("existingAppConfigName");
var existingAppConfigResourceGroup = builder.AddParameter("existingAppConfigResourceGroup");
var appConfig = builder.AddAzureAppConfiguration("app-config")
.AsExisting(existingAppConfigName, existingAppConfigResourceGroup);
builder.AddProject<Projects.ExampleProject>()
.WithReference(appConfig);
// After adding all resources, run the app...

For more information on treating Azure App Configuration resources as existing resources, see Use existing Azure resources.

When you publish your app, Aspire provisioning APIs generate Bicep alongside the manifest file. Bicep is a domain-specific language for defining Azure resources. For more information, see Bicep Overview.

When you add an Azure App Configuration resource, Bicep is generated that provisions the resource. 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 are reflected in the generated files.

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 API:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var appConfig = builder.AddAzureAppConfiguration("app-config");
appConfig.ConfigureInfrastructure(infra =>
{
var configStore = infra.GetProvisionableResources()
.OfType<AppConfigurationStore>()
.Single();
configStore.Sku = new AppConfigurationStoreSku
{
Name = "Standard"
};
configStore.Tags["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 resource is retrieved.
    • The Sku object has its name property set to Standard.
    • A tag is added to the App Configuration resource with a key of ExampleKey and a value of Example value.

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

The Aspire Azure App Configuration client integration is used to connect to an Azure App Configuration service. To get started with the Aspire Azure App Configuration client integration, install the 📦 Aspire.Azure.Data.AppConfiguration NuGet package.

.NET CLI — Add Aspire.Azure.Data.AppConfiguration package
dotnet add package Aspire.Azure.Data.AppConfiguration

In the Program.cs file of your client-consuming project, call the AddAzureAppConfiguration extension method to register a ConfigurationClient for use via the dependency injection container. The method takes a connection name parameter:

builder.AddAzureAppConfiguration(connectionName: "app-config");

After adding the ConfigurationClient, you can retrieve the client instance using dependency injection:

public class ExampleService(ConfigurationClient client)
{
// Use client...
}

For more information, see:

There might be situations where you want to register multiple ConfigurationClient instances with different connection names. To register keyed App Configuration clients, call the AddKeyedAzureAppConfiguration method:

builder.AddKeyedAzureAppConfiguration(name: "primary-config");
builder.AddKeyedAzureAppConfiguration(name: "secondary-config");

Then you can retrieve the client instances using dependency injection:

public class ExampleService(
[KeyedService("primary-config")] ConfigurationClient primaryClient,
[KeyedService("secondary-config")] ConfigurationClient secondaryClient)
{
// Use clients...
}

For more information, see Keyed services in .NET.

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. Either an Endpoint or a ConnectionString must be supplied.

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

builder.AddAzureAppConfiguration(connectionName: "app-config");

The connection information is retrieved from the ConnectionStrings configuration section. Two connection formats are supported:

  • Service endpoint (recommended): Uses the service endpoint with DefaultAzureCredential.

    {
    "ConnectionStrings": {
    "app-config": "https://{account_name}.azconfig.io"
    }
    }
  • Connection string: Includes an access key.

    {
    "ConnectionStrings": {
    "app-config": "Endpoint=https://{account_name}.azconfig.io;Id={id};Secret={secret}"
    }
    }

The library supports Microsoft.Extensions.Configuration. It loads settings from configuration using the Aspire:Azure:Data:AppConfiguration key:

{
"Aspire": {
"Azure": {
"Data": {
"AppConfiguration": {
"DisableHealthChecks": true,
"DisableTracing": false
}
}
}
}
}

You can configure settings inline:

builder.AddAzureAppConfiguration(
"app-config",
settings => settings.DisableHealthChecks = true);

Aspire integrations automatically set up Logging, Tracing, and Metrics configurations.

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

  • Azure
  • Azure.Core
  • Azure.Identity
  • Azure.Data.AppConfiguration

The Aspire Azure App Configuration integration will emit the following tracing activities using OpenTelemetry:

  • Azure.Data.AppConfiguration.*

The Aspire Azure App Configuration integration currently doesn’t support metrics by default due to limitations with the Azure SDK for .NET.

질문 & 답변협업커뮤니티토론보기