Skip to content

Commit 11ba9fe

Browse files
adamintAdam Ratzman
andauthored
avoid duplicate evaluation in ResolveConnectionStringReferenceAsync (#7714)
Co-authored-by: Adam Ratzman <[email protected]>
1 parent a1136f8 commit 11ba9fe

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/Aspire.Hosting/ApplicationModel/ConnectionStringReference.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ public class ConnectionStringReference(IResourceWithConnectionString resource, b
2727

2828
if (string.IsNullOrEmpty(value) && !Optional)
2929
{
30-
throw new DistributedApplicationException($"The connection string for the resource '{Resource.Name}' is not available.");
30+
ThrowConnectionStringUnavailableException();
3131
}
3232

3333
return value;
3434
}
35+
36+
internal void ThrowConnectionStringUnavailableException() => throw new DistributedApplicationException($"The connection string for the resource '{Resource.Name}' is not available.");
3537
}

src/Aspire.Hosting/ApplicationModel/ExpressionResolver.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,14 @@ async Task<ResolvedValue> ResolveConnectionStringReferenceAsync(ConnectionString
156156
{
157157
// We are substituting our own logic for ConnectionStringReference's GetValueAsync.
158158
// However, ConnectionStringReference#GetValueAsync will throw if the connection string is not optional but is not present.
159-
// To avoid duplicating that logic, we can defer to ConnectionStringReference#GetValueAsync, which will throw if needed.
160-
await ((IValueProvider)cs).GetValueAsync(cancellationToken).ConfigureAwait(false);
159+
// so we need to do the same here.
160+
var value = await ResolveInternalAsync(cs.Resource.ConnectionStringExpression).ConfigureAwait(false);
161+
if (string.IsNullOrEmpty(value.Value) && !cs.Optional)
162+
{
163+
cs.ThrowConnectionStringUnavailableException();
164+
}
161165

162-
return await ResolveInternalAsync(cs.Resource.ConnectionStringExpression).ConfigureAwait(false);
166+
return value;
163167
}
164168

165169
/// <summary>

0 commit comments

Comments
 (0)