You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update documentation for new polyglot environment variables (issue #5308) (#5313)
* Initial plan
* Update documentation with new polyglot environment variable format
Co-authored-by: IEvangelist <7679720+IEvangelist@users.noreply.github.com>
* Add clarification about resource names in environment variables
Co-authored-by: IEvangelist <7679720+IEvangelist@users.noreply.github.com>
* Improve clarity of note about connection name parameter
Co-authored-by: IEvangelist <7679720+IEvangelist@users.noreply.github.com>
* Update docs/fundamentals/app-host-overview.md
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: IEvangelist <7679720+IEvangelist@users.noreply.github.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
Adding a reference to the "apiservice" project results in service discovery environment variables being added to the frontend. This is because typically, project-to-project communication occurs over HTTP/gRPC. For more information, see [Aspire service discovery](../service-discovery/overview.md).
222
+
Adding a reference to the "apiservice" project results in service discovery environment variables being added to the frontend. This is because typically, project-to-project communication occurs over HTTP/gRPC.
223
+
224
+
Aspire injects two types of environment variables for service references:
225
+
226
+
-**Simplified format** (e.g., `APISERVICE_HTTP`): Uses the pattern `{RESOURCENAME}_{ENDPOINTNAME}` in uppercase. This format is simpler and more suitable for non-.NET languages and polyglot scenarios.
227
+
-**.NET service discovery format** (e.g., `services__apiservice__http__0`): Uses the pattern `services__{servicename}__{endpointname}__{index}` in lowercase. This format is used by .NET's configuration-based service discovery.
228
+
229
+
For more information, see [Aspire service discovery](../service-discovery/overview.md).
223
230
224
231
To get specific endpoints from a <xref:Aspire.Hosting.ApplicationModel.ContainerResource> or an <xref:Aspire.Hosting.ApplicationModel.ExecutableResource>, use one of the following endpoint APIs:
225
232
@@ -243,31 +250,68 @@ var apiservice = builder.AddProject<Projects.AspireApp_ApiService>("apiservice")
The `port` parameter is the port that the container is listening on. For more information on container ports, see [Container ports](networking-overview.md#container-ports). For more information on service discovery, see [Aspire service discovery](../service-discovery/overview.md).
249
256
250
257
### Service endpoint environment variable format
251
258
252
-
In the preceding section, the <xref:Aspire.Hosting.ResourceBuilderExtensions.WithReference*> method is used to express dependencies between resources. When service endpoints result in environment variables being injected into the dependent resource, the format might not be obvious. This section provides details on this format.
259
+
In the preceding section, the <xref:Aspire.Hosting.ResourceBuilderExtensions.WithReference*> method is used to express dependencies between resources. When service endpoints result in environment variables being injected into the dependent resource, the format might not be obvious. This section provides details on the available formats.
260
+
261
+
When one resource depends on another resource, the AppHost injects environment variables into the dependent resource. These environment variables configure the dependent resource to connect to the resource it depends on. Aspire provides two environment variable formats to support different scenarios:
253
262
254
-
When one resource depends on another resource, the AppHost injects environment variables into the dependent resource. These environment variables configure the dependent resource to connect to the resource it depends on. The format of the environment variables is specific to Aspire and expresses service endpoints in a way that is compatible with [Service Discovery](../service-discovery/overview.md) and polyglot scenarios.
263
+
#### Simplified format (polyglot-friendly)
255
264
256
-
Service endpoint environment variable names follow the pattern `{RESOURCENAME}_{ENDPOINTNAME}`, where both the resource name and endpoint name are uppercased. This format is language-agnostic and works well with non-.NET technologies.
265
+
The simplified format uses the pattern `{RESOURCENAME}_{ENDPOINTNAME}` in uppercase. This format is easier to use from non-.NET languages and is recommended for polyglot scenarios.
257
266
258
267
Consider the following environment variable examples:
259
268
260
269
```Environment
261
270
APISERVICE_HTTP
271
+
APISERVICE_HTTPS
262
272
```
263
273
264
-
The preceding environment variable expresses the HTTP endpoint for the `apiservice` service. The value of the environment variable is the URL of the service endpoint. A named endpoint might be expressed as follows:
274
+
The preceding environment variables express HTTP and HTTPS endpoints for the `apiservice` service. A named endpoint might be expressed as follows:
265
275
266
276
```Environment
267
277
APISERVICE_MYENDPOINT
268
278
```
269
279
270
-
In the preceding example, the `apiservice` service has a named endpoint called `myendpoint`. The value of the environment variable is the URL of the service endpoint.
280
+
In the preceding example, the `apiservice` service has a named endpoint called `myendpoint`.
281
+
282
+
> [!NOTE]
283
+
> The environment variable name is based on the resource name, not the optional connection name parameter. Even when using `WithReference(resource, "customname")` to specify a custom connection name, the generated environment variables still use the resource's name (e.g., `APISERVICE_HTTP`), not the custom name.
284
+
285
+
#### .NET service discovery format
286
+
287
+
The .NET service discovery format is used by .NET's configuration-based service discovery. Service endpoint environment variable names are prefixed with `services__` (double underscore), then the service name, the endpoint name, and finally the index. The index supports multiple endpoints for a single service, starting with `0` for the first endpoint and incrementing for each endpoint.
288
+
289
+
Consider the following environment variable examples:
290
+
291
+
```Environment
292
+
services__apiservice__http__0
293
+
services__apiservice__https__0
294
+
```
295
+
296
+
The preceding environment variables express the first HTTP and HTTPS endpoints for the `apiservice` service. A named endpoint might be expressed as follows:
297
+
298
+
```Environment
299
+
APISERVICE_MYENDPOINT
300
+
```
301
+
302
+
In the preceding example, the `apiservice` service has a named endpoint called `myendpoint`.
303
+
304
+
#### Using a specific endpoint with WithEnvironment
305
+
306
+
To specify a custom environment variable name for a specific endpoint, use the <xref:Aspire.Hosting.ResourceBuilderExtensions.WithEnvironment%2A> method combined with <xref:Aspire.Hosting.ResourceBuilderExtensions.GetEndpoint*>:
Copy file name to clipboardExpand all lines: docs/fundamentals/orchestrate-resources.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -285,7 +285,7 @@ var frontend = builder.AddProject<Projects.Frontend>("frontend")
285
285
.WithReference(api);
286
286
```
287
287
288
-
This configuration injects an environment variable like `API_HTTPS=https://api.example.com/` into the frontend project, enabling service discovery through the standard .NET service discovery mechanisms.
288
+
This configuration injects environment variables like `API_HTTPS=https://api.example.com/`and `services__api__https__0=https://api.example.com/`into the frontend project, enabling service discovery through both simplified and .NET-specific service discovery mechanisms.
0 commit comments