Skip to content

Commit bc97b33

Browse files
authored
External Services with URL Parameter fails to generate manifests (#10790)
* 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 * Update playground manifest
1 parent 8c9ad9d commit bc97b33

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

playground/ExternalServices/ExternalServices.AppHost/aspire-manifest.json

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,29 @@
4141
},
4242
"frontend": {
4343
"type": "project.v0",
44-
"path": "../WebFrontEnd/WebFrontEnd.csproj"
44+
"path": "../WebFrontEnd/WebFrontEnd.csproj",
45+
"env": {
46+
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES": "true",
47+
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
48+
"OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY": "in_memory",
49+
"ASPNETCORE_FORWARDEDHEADERS_ENABLED": "true",
50+
"HTTP_PORTS": "{frontend.bindings.http.targetPort}",
51+
"services__nuget__https__0": "https://api.nuget.org/",
52+
"EXTERNAL_SERVICE_URL": "{external-service-url.value}",
53+
"services__gateway__http__0": "{gateway.bindings.http.url}"
54+
},
55+
"bindings": {
56+
"http": {
57+
"scheme": "http",
58+
"protocol": "tcp",
59+
"transport": "http"
60+
},
61+
"https": {
62+
"scheme": "https",
63+
"protocol": "tcp",
64+
"transport": "http"
65+
}
66+
}
67+
}
68+
}
69+
}

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,23 @@ public async Task ExternalServiceWithParameterHttpHealthCheckResolvesUrlAsync()
443443
Assert.Contains(healthCheckKey, result.Entries.Keys);
444444
}
445445

446+
[Fact]
447+
public async Task ExternalServiceWithParameterPublishManifest()
448+
{
449+
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
450+
451+
var urlParam = builder.AddParameter("external-url");
452+
var externalService = builder.AddExternalService("external", urlParam);
453+
454+
var project = builder.AddProject<TestProject>("project")
455+
.WithReference(externalService)
456+
.WithEnvironment("EXTERNAL_SERVICE", externalService);
457+
458+
var manifest = await ManifestUtils.GetManifest(project.Resource);
459+
460+
await Verify(manifest.ToString(), extension: "json");
461+
}
462+
446463
private sealed class TestProject : IProjectMetadata
447464
{
448465
public string ProjectPath => "testproject";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"type": "project.v0",
3+
"path": "testproject",
4+
"env": {
5+
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES": "true",
6+
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
7+
"OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY": "in_memory",
8+
"services__external__default__0": "{external-url.value}",
9+
"EXTERNAL_SERVICE": "{external-url.value}"
10+
}
11+
}

0 commit comments

Comments
 (0)