@@ -7,71 +7,25 @@ namespace Aspire.Hosting.ApplicationModel;
7
7
8
8
internal class ExpressionResolver ( string containerHostName , CancellationToken cancellationToken , bool sourceIsContainer )
9
9
{
10
- class HostAndPortPresence
11
- {
12
- public bool HasHost { get ; set ; }
13
- public bool HasPort { get ; set ; }
14
- }
15
-
16
- // For each endpoint, keep track of whether host and port are in use
17
- // The key is the unique name of the endpoint, which is the resource name and endpoint name
18
- readonly Dictionary < string , HostAndPortPresence > _endpointUsage = [ ] ;
19
- static string EndpointUniqueName ( EndpointReference endpointReference ) => $ "{ endpointReference . Resource . Name } /{ endpointReference . EndpointName } ";
20
-
21
- // This marks whether we are in the preprocess phase or not
22
- // Not thread-safe, but we doesn't matter, since this class is never used concurrently
23
- bool Preprocess { get ; set ; }
24
10
25
11
async Task < string ? > EvalEndpointAsync ( EndpointReference endpointReference , EndpointProperty property )
26
12
{
27
- var endpointUniqueName = EndpointUniqueName ( endpointReference ) ;
28
-
29
- // In the preprocess phase, our only goal is to determine if the host and port properties are both used
30
- // for each endpoint.
31
- if ( Preprocess )
32
- {
33
- if ( ! _endpointUsage . TryGetValue ( endpointUniqueName , out var hostAndPortPresence ) )
34
- {
35
- hostAndPortPresence = new HostAndPortPresence ( ) ;
36
- _endpointUsage [ endpointUniqueName ] = hostAndPortPresence ;
37
- }
38
-
39
- if ( property is EndpointProperty . Host or EndpointProperty . IPV4Host )
40
- {
41
- hostAndPortPresence . HasHost = true ;
42
- }
43
- else if ( property == EndpointProperty . Port )
44
- {
45
- hostAndPortPresence . HasPort = true ;
46
- }
47
- else if ( property is EndpointProperty . Url or EndpointProperty . HostAndPort )
48
- {
49
- hostAndPortPresence . HasHost = hostAndPortPresence . HasPort = true ;
50
- }
51
- return string . Empty ;
52
- }
53
13
// We need to use the root resource, e.g. AzureStorageResource instead of AzureBlobResource
54
14
// Otherwise, we get the wrong values for IsContainer and Name
55
15
var target = endpointReference . Resource . GetRootResource ( ) ;
56
16
57
- bool HasBothHostAndPort ( ) =>
58
- _endpointUsage [ endpointUniqueName ] . HasHost &&
59
- _endpointUsage [ endpointUniqueName ] . HasPort ;
60
-
61
- return ( property , target . IsContainer ( ) , HasBothHostAndPort ( ) ) switch
17
+ return ( property , target . IsContainer ( ) ) switch
62
18
{
63
19
// If Container -> Container, we go directly to the container name and target port, bypassing the host
64
- // But only do this if we have processed both the host and port properties for that same endpoint.
65
- // This allows the host and port to be handled in a unified way.
66
- ( EndpointProperty . Host or EndpointProperty . IPV4Host , true , true ) => target . Name ,
67
- ( EndpointProperty . Port , true , true ) => await endpointReference . Property ( EndpointProperty . TargetPort ) . GetValueAsync ( cancellationToken ) . ConfigureAwait ( false ) ,
20
+ ( EndpointProperty . Host or EndpointProperty . IPV4Host , true ) => target . Name ,
21
+ ( EndpointProperty . Port , true ) => await endpointReference . Property ( EndpointProperty . TargetPort ) . GetValueAsync ( cancellationToken ) . ConfigureAwait ( false ) ,
68
22
// If Container -> Exe, we need to go through the container host
69
- ( EndpointProperty . Host or EndpointProperty . IPV4Host , false , _ ) => containerHostName ,
70
- ( EndpointProperty . Url , _ , _ ) => string . Format ( CultureInfo . InvariantCulture , "{0}://{1}:{2}" ,
23
+ ( EndpointProperty . Host or EndpointProperty . IPV4Host , false ) => containerHostName ,
24
+ ( EndpointProperty . Url , _ ) => string . Format ( CultureInfo . InvariantCulture , "{0}://{1}:{2}" ,
71
25
endpointReference . Scheme ,
72
26
await EvalEndpointAsync ( endpointReference , EndpointProperty . Host ) . ConfigureAwait ( false ) ,
73
27
await EvalEndpointAsync ( endpointReference , EndpointProperty . Port ) . ConfigureAwait ( false ) ) ,
74
- ( EndpointProperty . HostAndPort , _ , _ ) => string . Format ( CultureInfo . InvariantCulture , "{0}:{1}" ,
28
+ ( EndpointProperty . HostAndPort , _ ) => string . Format ( CultureInfo . InvariantCulture , "{0}:{1}" ,
75
29
await EvalEndpointAsync ( endpointReference , EndpointProperty . Host ) . ConfigureAwait ( false ) ,
76
30
await EvalEndpointAsync ( endpointReference , EndpointProperty . Port ) . ConfigureAwait ( false ) ) ,
77
31
_ => await endpointReference . Property ( property ) . GetValueAsync ( cancellationToken ) . ConfigureAwait ( false )
@@ -159,8 +113,8 @@ async Task<ResolvedValue> ResolveConnectionStringReferenceAsync(ConnectionString
159
113
// so we need to do the same here.
160
114
var value = await ResolveInternalAsync ( cs . Resource . ConnectionStringExpression ) . ConfigureAwait ( false ) ;
161
115
162
- // While pre-processing the endpoints, we never throw
163
- if ( ! Preprocess && string . IsNullOrEmpty ( value . Value ) && ! cs . Optional )
116
+ // Throw if the connection string is required but not present
117
+ if ( string . IsNullOrEmpty ( value . Value ) && ! cs . Optional )
164
118
{
165
119
cs . ThrowConnectionStringUnavailableException ( ) ;
166
120
}
@@ -188,12 +142,6 @@ async ValueTask<ResolvedValue> ResolveInternalAsync(object? value)
188
142
static async ValueTask < ResolvedValue > ResolveWithContainerSourceAsync ( IValueProvider valueProvider , string containerHostName , bool sourceIsContainer , CancellationToken cancellationToken )
189
143
{
190
144
var resolver = new ExpressionResolver ( containerHostName , cancellationToken , sourceIsContainer ) ;
191
-
192
- // Run the processing phase to know if the host and port properties are both used for each endpoint.
193
- resolver . Preprocess = true ;
194
- await resolver . ResolveInternalAsync ( valueProvider ) . ConfigureAwait ( false ) ;
195
- resolver . Preprocess = false ;
196
-
197
145
return await resolver . ResolveInternalAsync ( valueProvider ) . ConfigureAwait ( false ) ;
198
146
}
199
147
0 commit comments