Azure App Configuration 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 an Azure configuration provider to populate the IConfiguration instance.
dotnet add package Aspire.Microsoft.Extensions.Configuration.AzureAppConfiguration#:package Aspire.Microsoft.Extensions.Configuration.AzureAppConfiguration@*<PackageReference Include="Aspire.Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="*" />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");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"];}Configure the Azure App Configuration provider
Section titled “Configure the Azure App Configuration provider”The AddAzureAppConfiguration method accepts an optional Action<AzureAppConfigurationOptions> configureOptions delegate that you use to configure the Azure App Configuration provider. This follows the same pattern as the non-Aspire Microsoft.Extensions.Configuration.AzureAppConfiguration package, but Aspire automatically handles the connection—you don’t need to call options.Connect.
builder.AddAzureAppConfiguration( "config", configureOptions: options => { // Select specific keys or labels options.Select("MyApp:*"); options.Select("MyApp:*", "Production");
// Configure refresh options options.ConfigureRefresh(refresh => { refresh.Register("MyApp:Sentinel", refreshAll: true) .SetRefreshInterval(TimeSpan.FromSeconds(30)); }); });For more information on available configuration options, see the Azure App Configuration provider reference.
Use feature flags
Section titled “Use feature flags”To use feature flags, install the 📦 Microsoft.FeatureManagement NuGet package:
dotnet add package Microsoft.FeatureManagement#:package Microsoft.FeatureManagement@*<PackageReference Include="Microsoft.FeatureManagement" Version="*" />App Configuration doesn’t load feature flags by default. To load feature flags, use the configureOptions delegate (as shown in Configure the Azure App Configuration provider) to call UseFeatureFlags() when calling builder.AddAzureAppConfiguration.
builder.AddAzureAppConfiguration( "config", configureOptions: options => options.UseFeatureFlags());
// Register feature management servicesbuilder.Services.AddFeatureManagement();The configureOptions pattern is used to configure the Azure App Configuration provider. For more information, see Azure App Configuration: .NET configuration provider. 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 servicesbuilder.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
Section titled “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
Section titled “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
Section titled “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
Section titled “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 = "https://YOUR_URI");Logging
Section titled “Logging”The Aspire Azure App Configuration integration uses the following log categories:
Microsoft.Extensions.Configuration.AzureAppConfiguration.Refresh
Tracing
Section titled “Tracing”The Aspire Azure App Configuration integration doesn’t make use of any activity sources thus no tracing is available.
Metrics
Section titled “Metrics”The Aspire Azure App Configuration integration currently doesn’t support metrics.