Skip to content

Commit 0f19a21

Browse files
[release/9.4] External Services with URL Parameter fails to generate manifests (#10806)
* External Services with URL Parameter fails to generate manifests Need to check for publish mode before calling GetValueAsync on the URL parameter Fix #10789 * Add test * Fix test to not use Verify --------- Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
1 parent d3774ae commit 0f19a21

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/Aspire.Hosting/ResourceBuilderExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ public static IResourceBuilder<T> WithEnvironment<T>(this IResourceBuilder<T> bu
179179
{
180180
builder.WithEnvironment(async context =>
181181
{
182-
var url = await externalService.Resource.UrlParameter.GetValueAsync(context.CancellationToken).ConfigureAwait(false);
183-
184182
// In publish mode we can't validate the parameter value so we'll just use it without validating.
185-
if (!context.ExecutionContext.IsPublishMode && !ExternalServiceResource.UrlIsValidForExternalService(url, out var _, out var message))
183+
if (!context.ExecutionContext.IsPublishMode)
186184
{
187-
throw new DistributedApplicationException($"The URL parameter '{externalService.Resource.UrlParameter.Name}' for the external service '{externalService.Resource.Name}' is invalid: {message}");
185+
var url = await externalService.Resource.UrlParameter.GetValueAsync(context.CancellationToken).ConfigureAwait(false);
186+
if (!ExternalServiceResource.UrlIsValidForExternalService(url, out var _, out var message))
187+
{
188+
throw new DistributedApplicationException($"The URL parameter '{externalService.Resource.UrlParameter.Name}' for the external service '{externalService.Resource.Name}' is invalid: {message}");
189+
}
188190
}
189191

190192
context.EnvironmentVariables[name] = externalService.Resource.UrlParameter;

tests/Aspire.Hosting.Tests/ExternalServiceTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,36 @@ public async Task ExternalServiceWithParameterHttpHealthCheckResolvesUrlAsync()
444444
Assert.Contains(healthCheckKey, result.Entries.Keys);
445445
}
446446

447+
[Fact]
448+
public async Task ExternalServiceWithParameterPublishManifest()
449+
{
450+
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
451+
452+
var urlParam = builder.AddParameter("external-url");
453+
var externalService = builder.AddExternalService("external", urlParam);
454+
455+
var project = builder.AddProject<TestProject>("project")
456+
.WithReference(externalService)
457+
.WithEnvironment("EXTERNAL_SERVICE", externalService);
458+
459+
var manifest = await ManifestUtils.GetManifest(project.Resource);
460+
461+
Assert.Equal(
462+
"""
463+
{
464+
"type": "project.v0",
465+
"path": "testproject",
466+
"env": {
467+
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES": "true",
468+
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
469+
"OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY": "in_memory",
470+
"services__external__default__0": "{external-url.value}",
471+
"EXTERNAL_SERVICE": "{external-url.value}"
472+
}
473+
}
474+
""", manifest.ToString());
475+
}
476+
447477
private sealed class TestProject : IProjectMetadata
448478
{
449479
public string ProjectPath => "testproject";

0 commit comments

Comments
 (0)