@@ -134,7 +134,8 @@ private static void AddProjectReferenceForRunMode<T>(
134134 where T : IResourceWithEndpoints , IResourceWithEnvironment , IComputeResource
135135 {
136136 // Run mode waits for the project to provision so the agent's connection variables resolve before it starts.
137- AddProjectReference ( builder , project , waitForProject : true ) ;
137+ // Use the encoded connection name so run and publish emit identical variable names (see EncodeProjectConnectionName).
138+ builder . WithProjectReference ( project , connectionName : EncodeProjectConnectionName ( project ) , waitFor : true ) ;
138139
139140 // The default ACR is required for publish-time image push, but in run mode it adds noise to the dashboard.
140141 // When a hosted agent references a Foundry project for local execution, remove the default registry resource.
@@ -145,36 +146,14 @@ private static void AddProjectReferenceForRunMode<T>(
145146 }
146147 }
147148
148- // Injects the Foundry project reference into the compute resource so the agent can reach the project
149- // via the standard Aspire reference environment variables (ConnectionStrings__{name}, {NAME}_URI, ...).
150- //
151- // This must produce identical variable names in run and publish mode so an app that reads them locally
152- // keeps working once deployed. The connection-string variable is named "ConnectionStrings__{connectionName}".
153- // Foundry validates hosted agent environment variable names against ^[A-Za-z0-9_]+$ at deploy time (see
154- // HostedAgentConfiguration), so a project name containing '-' — including the auto-generated default
155- // "{resource.Name}-proj" — would otherwise emit "ConnectionStrings__{name}-proj" and fail deployment.
156- // Encode the connection name up front so both modes stay symmetric and deploy-safe; for already-valid names
157- // (e.g. "myproject") this is a no-op.
158- //
159- // We deliberately call the general WithReference overload (with an explicit connectionName) rather than the
160- // Foundry-specific WithReference(project) overload: the latter ignores connectionName and always emits the
161- // raw resource name. The Foundry overload also implicitly applies WaitFor(project); we reproduce that here
162- // for run mode via waitForProject, but skip it in publish mode where waiting has no meaning.
163- private static IResourceBuilder < T > AddProjectReference < T > (
164- IResourceBuilder < T > builder ,
165- IResourceBuilder < AzureCognitiveServicesProjectResource > project ,
166- bool waitForProject )
167- where T : IResourceWithEndpoints , IResourceWithEnvironment , IComputeResource
168- {
169- builder . WithReference ( project , connectionName : EnvironmentVariableNameEncoder . Encode ( project . Resource . Name ) ) ;
170-
171- if ( waitForProject && builder is IResourceBuilder < IResourceWithWaitSupport > waitableBuilder )
172- {
173- waitableBuilder . WaitFor ( project ) ;
174- }
175-
176- return builder ;
177- }
149+ // Encodes the project's connection name so the resulting reference environment variables stay deploy-safe.
150+ // A project reference emits "ConnectionStrings__{connectionName}" (plus "{NAME}_URI", ...). Foundry validates
151+ // hosted agent environment variable names against ^[A-Za-z0-9_]+$ at deploy time (see HostedAgentConfiguration),
152+ // so a project name containing '-' — including the auto-generated default "{resource.Name}-proj" — would
153+ // otherwise emit "ConnectionStrings__{name}-proj" and fail deployment. Encoding up front keeps run and publish
154+ // mode symmetric and deploy-safe; for already-valid names (e.g. "myproject") this is a no-op.
155+ private static string EncodeProjectConnectionName ( IResourceBuilder < AzureCognitiveServicesProjectResource > project )
156+ => EnvironmentVariableNameEncoder . Encode ( project . Resource . Name ) ;
178157
179158 private static IResourceBuilder < AzureCognitiveServicesProjectResource > ResolveProjectBuilderForPublish < T > ( IResourceBuilder < T > builder )
180159 where T : IResourceWithEndpoints , IResourceWithEnvironment , IComputeResource
@@ -375,7 +354,7 @@ private static void ConfigurePublishMode<T>(
375354 // needed during environment variable resolution in the deployment phase, and
376355 // mirrors the reference added in run mode so the variables match in both modes.
377356 // WaitFor is omitted in publish mode where it has no meaning.
378- AddProjectReference ( builder , project , waitForProject : false ) ;
357+ builder . WithProjectReference ( project , connectionName : EncodeProjectConnectionName ( project ) , waitFor : false ) ;
379358
380359 builder . ApplicationBuilder . AddResource ( hostedAgent )
381360 . WithIconName ( "Agents" )
0 commit comments