-
Hi, I am using the aspire 'DatabaseContainers' sample but simplified by only spinning up the SQL Server database. When AppHost startup everything, the correct ConnectionString pointing to the Docker Desktop container is injected into the ApiService project as expected: But if I connect to our Azure AppConfiguration in the ApiService, then whatever is injected in the AppHost project gets overridden by Azure AppConfiguration. So calling an enpoint like app.MapGet("/connectionstring", () => Results.Ok(app.Configuration.GetConnectionString("Pida") ?? "No ConnectionString called Pida found anywhere!")); returns this ConnectionString coming from Azure AppConfiguration:
Instead of the one injected to ApiService by the Aspire AppHost:
The placement of using Azure.Core;
using Azure.Extensions.AspNetCore.Configuration.Secrets;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using DatabaseContainers.ApiService;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
var builder = WebApplication.CreateBuilder(args);
string appConfig = builder.Configuration.GetValue<string>("AppConfig") ??
throw new InvalidOperationException("AppConfig uri is missing from configuration/environment");
string uriString = builder.Configuration.GetValue<string>("KeyVault") ??
throw new InvalidOperationException("KeyVault uri is missing from configuration/environment");
builder.Configuration.AddAzureAppConfiguration((Action<AzureAppConfigurationOptions>)(options => options.Connect(new Uri(appConfig), (TokenCredential)new DefaultAzureCredential()).Select("*").Select("*", "local").ConfigureRefresh((Action<AzureAppConfigurationRefreshOptions>)(refreshOptions => refreshOptions.Register("Sentinel", "\0", true).SetCacheExpiration(TimeSpan.FromSeconds(15.0)))))).AddAzureKeyVault(new SecretClient(new Uri(uriString), (TokenCredential)new DefaultAzureCredential()), new KeyVaultSecretManager());
// Add service defaults & Aspire components.
builder.AddServiceDefaults();
builder.AddSqlServerClient("AddressBook");
// Add services to the container.
builder.Services.AddProblemDetails();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseExceptionHandler();
app.UseSwagger();
if (app.Environment.IsDevelopment())
{
app.UseSwaggerUI();
}
app.MapAddressBookApi();
app.MapDefaultEndpoints();
app.Run(); I was under the impression that we can easily add .NET Aspire orchestration to our existing apps during development, and by simply injecting the right environment variables through the AppHost the app could then use a SQL Server container in Docker Desktop, but later when app is deployed to Kubernetes without the AppHost project, our app will simply get the "Pida" ConnectionString from Azure AppConfiguration. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This is how the .NET configuration system works. Providers are ordered and you’re adding app configuration last. Aspire isn’t doing anything magical here. |
Beta Was this translation helpful? Give feedback.
There's no aspire configuration provider. The apphost is setting environment variables which is the second last provider added by default.
By adding AddEnvironmentVariables after adding the app configuration based configuration.