diff --git a/src/Aspire.Hosting/DistributedApplicationBuilder.cs b/src/Aspire.Hosting/DistributedApplicationBuilder.cs index cb424ba7cb8..0c338160625 100644 --- a/src/Aspire.Hosting/DistributedApplicationBuilder.cs +++ b/src/Aspire.Hosting/DistributedApplicationBuilder.cs @@ -80,12 +80,13 @@ public DistributedApplicationBuilder(DistributedApplicationOptions options) AppHostDirectory = options.ProjectDirectory ?? _innerBuilder.Environment.ContentRootPath; + // Set configuration + ConfigurePublishingOptions(options); _innerBuilder.Configuration.AddInMemoryCollection(new Dictionary { // Make the app host directory available to the application via configuration ["AppHost:Directory"] = AppHostDirectory }); - if (!IsOtlpApiKeyAuthDisabled(_innerBuilder.Configuration)) { // Set a random API key for the OTLP exporter. @@ -97,7 +98,6 @@ public DistributedApplicationBuilder(DistributedApplicationOptions options) } ); } - if (!options.DisableDashboard) { // Set a random API key for the OTLP exporter. @@ -136,7 +136,6 @@ public DistributedApplicationBuilder(DistributedApplicationOptions options) _innerBuilder.Services.AddSingleton(); // Publishing support - ConfigurePublishingOptions(options); _innerBuilder.Services.AddLifecycleHook(); _innerBuilder.Services.AddKeyedSingleton("manifest"); _innerBuilder.Services.AddKeyedSingleton("dcp"); diff --git a/tests/Aspire.Hosting.Tests/ProjectResourceTests.cs b/tests/Aspire.Hosting.Tests/ProjectResourceTests.cs index 29d0e2ba3c1..38baad1cad0 100644 --- a/tests/Aspire.Hosting.Tests/ProjectResourceTests.cs +++ b/tests/Aspire.Hosting.Tests/ProjectResourceTests.cs @@ -5,7 +5,6 @@ using Aspire.Hosting.Tests.Helpers; using Aspire.Hosting.Tests.Utils; using Aspire.Hosting.Utils; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Xunit; @@ -91,11 +90,7 @@ public async Task AddProjectAddsEnvironmentVariablesAndServiceMetadata() [InlineData(null, true)] public async Task AddProjectAddsEnvironmentVariablesAndServiceMetadata_OtlpAuthDisabledSetting(string? value, bool hasHeader) { - var appBuilder = CreateBuilder(); - appBuilder.Configuration.AddInMemoryCollection(new Dictionary - { - ["DOTNET_DISABLE_OTLP_API_KEY_AUTH"] = value - }); + var appBuilder = CreateBuilder(args: [$"DOTNET_DISABLE_OTLP_API_KEY_AUTH={value}"]); appBuilder.AddProject("projectName", launchProfileName: null); using var app = appBuilder.Build(); @@ -108,7 +103,14 @@ public async Task AddProjectAddsEnvironmentVariablesAndServiceMetadata_OtlpAuthD var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(resource); - Assert.Equal(hasHeader, config.ContainsKey("OTEL_EXPORTER_OTLP_HEADERS")); + if (hasHeader) + { + Assert.True(config.ContainsKey("OTEL_EXPORTER_OTLP_HEADERS"), "Config should have 'OTEL_EXPORTER_OTLP_HEADERS' header and doesn't."); + } + else + { + Assert.False(config.ContainsKey("OTEL_EXPORTER_OTLP_HEADERS"), "Config shouldn't have 'OTEL_EXPORTER_OTLP_HEADERS' header and does."); + } } [Fact] @@ -282,10 +284,18 @@ public async Task VerifyManifest(bool disableForwardedHeaders) Assert.Equal(expectedManifest, manifest.ToString()); } - private static IDistributedApplicationBuilder CreateBuilder(DistributedApplicationOperation operation = DistributedApplicationOperation.Publish) + private static IDistributedApplicationBuilder CreateBuilder(string[]? args = null, DistributedApplicationOperation operation = DistributedApplicationOperation.Publish) { - var args = operation == DistributedApplicationOperation.Publish ? new[] { "--publisher", "manifest" } : Array.Empty(); - var appBuilder = DistributedApplication.CreateBuilder(args); + var resovledArgs = new List(); + if (args != null) + { + resovledArgs.AddRange(args); + } + if (operation == DistributedApplicationOperation.Publish) + { + resovledArgs.AddRange(["--publisher", "manifest"]); + } + var appBuilder = DistributedApplication.CreateBuilder(resovledArgs.ToArray()); // Block DCP from actually starting anything up as we don't need it for this test. appBuilder.Services.AddKeyedSingleton("manifest");