Skip to content

Commit 4c382cc

Browse files
Fix a regression in endpoint resolution in environment variables - Endpoints wrapped in connection strings were throwing during the preproces pass of the evalution. Avoid throwing during that pass - Added tests for this scenario (#8610)
Fixes #8596 Co-authored-by: David Fowler <davidfowl@gmail.com>
1 parent 0623631 commit 4c382cc

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/Aspire.Hosting/ApplicationModel/ExpressionResolver.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ async Task<ResolvedValue> ResolveConnectionStringReferenceAsync(ConnectionString
158158
// However, ConnectionStringReference#GetValueAsync will throw if the connection string is not optional but is not present.
159159
// so we need to do the same here.
160160
var value = await ResolveInternalAsync(cs.Resource.ConnectionStringExpression).ConfigureAwait(false);
161-
if (string.IsNullOrEmpty(value.Value) && !cs.Optional)
161+
162+
// While pre-processing the endpoints, we never throw
163+
if (!Preprocess && string.IsNullOrEmpty(value.Value) && !cs.Optional)
162164
{
163165
cs.ThrowConnectionStringUnavailableException();
164166
}

tests/Aspire.Hosting.Tests/ExpressionResolverTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,41 @@ public async Task HostUrlPropertyGetsResolvedInOtlpExporterEndpoint(bool contain
163163
var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(test.Resource, DistributedApplicationOperation.Run, TestServiceProvider.Instance, "ContainerHostName").DefaultTimeout();
164164
Assert.Equal(expectedValue, config["OTEL_EXPORTER_OTLP_ENDPOINT"]);
165165
}
166+
167+
[Fact]
168+
public async Task ContainerToContainerEndpointShouldResolve()
169+
{
170+
var builder = DistributedApplication.CreateBuilder();
171+
172+
var connectionStringResource = builder.AddResource(new MyContainerResource("myContainer"))
173+
.WithImage("redis")
174+
.WithHttpEndpoint(targetPort: 8080)
175+
.WithEndpoint("http", e =>
176+
{
177+
e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 8001, "ContainerHostName", "{{ targetPort }}");
178+
});
179+
180+
var dep = builder.AddContainer("container", "redis")
181+
.WithReference(connectionStringResource)
182+
.WaitFor(connectionStringResource);
183+
184+
var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(dep.Resource, DistributedApplicationOperation.Run, TestServiceProvider.Instance, "ContainerHostName").DefaultTimeout();
185+
186+
Assert.Equal("http://myContainer:8080", config["ConnectionStrings__myContainer"]);
187+
}
188+
}
189+
190+
sealed class MyContainerResource : ContainerResource, IResourceWithConnectionString
191+
{
192+
public MyContainerResource(string name) : base(name)
193+
{
194+
PrimaryEndpoint = new(this, "http");
195+
}
196+
197+
public EndpointReference PrimaryEndpoint { get; }
198+
199+
public ReferenceExpression ConnectionStringExpression =>
200+
ReferenceExpression.Create($"{PrimaryEndpoint.Property(EndpointProperty.Url)}");
166201
}
167202

168203
sealed class TestValueProviderResource(string name) : Resource(name), IValueProvider

0 commit comments

Comments
 (0)