Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,29 @@
},
"frontend": {
"type": "project.v0",
"path": "../WebFrontEnd/WebFrontEnd.csproj"
"path": "../WebFrontEnd/WebFrontEnd.csproj",
"env": {
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES": "true",
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
"OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY": "in_memory",
"ASPNETCORE_FORWARDEDHEADERS_ENABLED": "true",
"HTTP_PORTS": "{frontend.bindings.http.targetPort}",
"services__nuget__https__0": "https://api.nuget.org/",
"EXTERNAL_SERVICE_URL": "{external-service-url.value}",
"services__gateway__http__0": "{gateway.bindings.http.url}"
},
"bindings": {
"http": {
"scheme": "http",
"protocol": "tcp",
"transport": "http"
},
"https": {
"scheme": "https",
"protocol": "tcp",
"transport": "http"
}
}
}
}
}
10 changes: 6 additions & 4 deletions src/Aspire.Hosting/ResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,14 @@ public static IResourceBuilder<T> WithEnvironment<T>(this IResourceBuilder<T> bu
{
builder.WithEnvironment(async context =>
{
var url = await externalService.Resource.UrlParameter.GetValueAsync(context.CancellationToken).ConfigureAwait(false);

// In publish mode we can't validate the parameter value so we'll just use it without validating.
if (!context.ExecutionContext.IsPublishMode && !ExternalServiceResource.UrlIsValidForExternalService(url, out var _, out var message))
if (!context.ExecutionContext.IsPublishMode)
{
throw new DistributedApplicationException($"The URL parameter '{externalService.Resource.UrlParameter.Name}' for the external service '{externalService.Resource.Name}' is invalid: {message}");
var url = await externalService.Resource.UrlParameter.GetValueAsync(context.CancellationToken).ConfigureAwait(false);
if (!ExternalServiceResource.UrlIsValidForExternalService(url, out var _, out var message))
{
throw new DistributedApplicationException($"The URL parameter '{externalService.Resource.UrlParameter.Name}' for the external service '{externalService.Resource.Name}' is invalid: {message}");
}
}

context.EnvironmentVariables[name] = externalService.Resource.UrlParameter;
Expand Down
17 changes: 17 additions & 0 deletions tests/Aspire.Hosting.Tests/ExternalServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,23 @@ public async Task ExternalServiceWithParameterHttpHealthCheckResolvesUrlAsync()
Assert.Contains(healthCheckKey, result.Entries.Keys);
}

[Fact]
public async Task ExternalServiceWithParameterPublishManifest()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);

var urlParam = builder.AddParameter("external-url");
var externalService = builder.AddExternalService("external", urlParam);

var project = builder.AddProject<TestProject>("project")
.WithReference(externalService)
.WithEnvironment("EXTERNAL_SERVICE", externalService);

var manifest = await ManifestUtils.GetManifest(project.Resource);

await Verify(manifest.ToString(), extension: "json");
}

private sealed class TestProject : IProjectMetadata
{
public string ProjectPath => "testproject";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "project.v0",
"path": "testproject",
"env": {
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES": "true",
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
"OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY": "in_memory",
"services__external__default__0": "{external-url.value}",
"EXTERNAL_SERVICE": "{external-url.value}"
}
}
Loading