Use this file to discover all available pages before exploring further.
Configuration is a critical part of any distributed application. .NET Aspire provides multiple ways to configure your resources, from simple key-value pairs to complex hierarchical settings.
var builder = DistributedApplication.CreateBuilder(args);// Get a single valuevar apiKey = builder.Configuration["ExternalApi:ApiKey"];// Get a sectionvar apiConfig = builder.Configuration.GetSection("ExternalApi");var apiKey = apiConfig["ApiKey"];var baseUrl = apiConfig["BaseUrl"];
var api = builder.AddProject<Projects.Api>("api") .WithEnvironment("Logging__LogLevel__Default", "Debug") .WithEnvironment("Feature__EnableBeta", "true");
Configure resources differently based on the environment:
var builder = DistributedApplication.CreateBuilder(args);var isDevelopment = builder.Environment.IsDevelopment();var isProduction = builder.Environment.IsProduction();if (isDevelopment){ // Use local Redis var cache = builder.AddRedis("cache");}else{ // Use Azure Cache for Redis in production var cache = builder.AddAzureRedis("cache");}
When generating deployment manifests, configuration is handled differently:
var builder = DistributedApplication.CreateBuilder(args);if (builder.ExecutionContext.IsPublishMode){ // Configuration for deployment var externalDb = builder.Configuration.GetConnectionString("ProductionDb");}else{ // Configuration for local development var db = builder.AddPostgres("postgres").AddDatabase("catalogdb");}
var enableBeta = builder.Configuration.GetValue<bool>("Features:EnableBeta");if (enableBeta){ var betaService = builder.AddProject<Projects.BetaService>("beta-service");}
External Service URLs
Configure external services via parameters:
var paymentUrl = builder.AddParameter("payment-api-url");var paymentApi = builder.AddExternalService("payment-api", paymentUrl);var api = builder.AddProject<Projects.Api>("api") .WithReference(paymentApi);
Database Credentials
Use parameters for credentials:
var dbPassword = builder.AddParameter("db-password", secret: true);var db = builder.AddPostgres("postgres") .WithEnvironment("POSTGRES_PASSWORD", dbPassword) .AddDatabase("appdb");
Replica Count
Configure scaling via configuration:
var replicaCount = builder.Configuration.GetValue<int>("Api:Replicas", defaultValue: 1);var api = builder.AddProject<Projects.Api>("api") .WithReplicas(replicaCount);