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
4 changes: 3 additions & 1 deletion src/Aspire.Hosting/ApplicationModel/ExpressionResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ async Task<ResolvedValue> ResolveConnectionStringReferenceAsync(ConnectionString
// However, ConnectionStringReference#GetValueAsync will throw if the connection string is not optional but is not present.
// so we need to do the same here.
var value = await ResolveInternalAsync(cs.Resource.ConnectionStringExpression).ConfigureAwait(false);
if (string.IsNullOrEmpty(value.Value) && !cs.Optional)

// While pre-processing the endpoints, we never throw
if (!Preprocess && string.IsNullOrEmpty(value.Value) && !cs.Optional)
{
cs.ThrowConnectionStringUnavailableException();
}
Expand Down
35 changes: 35 additions & 0 deletions tests/Aspire.Hosting.Tests/ExpressionResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,41 @@ public async Task HostUrlPropertyGetsResolvedInOtlpExporterEndpoint(bool contain
var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(test.Resource, DistributedApplicationOperation.Run, TestServiceProvider.Instance, "ContainerHostName").DefaultTimeout();
Assert.Equal(expectedValue, config["OTEL_EXPORTER_OTLP_ENDPOINT"]);
}

[Fact]
public async Task ContainerToContainerEndpointShouldResolve()
{
var builder = DistributedApplication.CreateBuilder();

var connectionStringResource = builder.AddResource(new MyContainerResource("myContainer"))
.WithImage("redis")
.WithHttpEndpoint(targetPort: 8080)
.WithEndpoint("http", e =>
{
e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 8001, "ContainerHostName", "{{ targetPort }}");
});

var dep = builder.AddContainer("container", "redis")
.WithReference(connectionStringResource)
.WaitFor(connectionStringResource);

var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(dep.Resource, DistributedApplicationOperation.Run, TestServiceProvider.Instance, "ContainerHostName").DefaultTimeout();

Assert.Equal("http://myContainer:8080", config["ConnectionStrings__myContainer"]);
}
}

sealed class MyContainerResource : ContainerResource, IResourceWithConnectionString
{
public MyContainerResource(string name) : base(name)
{
PrimaryEndpoint = new(this, "http");
}

public EndpointReference PrimaryEndpoint { get; }

public ReferenceExpression ConnectionStringExpression =>
ReferenceExpression.Create($"{PrimaryEndpoint.Property(EndpointProperty.Url)}");
}

sealed class TestValueProviderResource(string name) : Resource(name), IValueProvider
Expand Down
Loading