Skip to content

Commit 42c6f38

Browse files
CopilotIEvangelist
andauthored
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>
1 parent 32c19fa commit 42c6f38

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

docs/deployment/azd/aca-deployment-azd-in-depth.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ properties:
374374
value: http://apiservice.internal.{{ .Env.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN }}
375375
- name: APISERVICE_HTTPS
376376
value: https://apiservice.internal.{{ .Env.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN }}
377+
- name: services__apiservice__http__0
378+
value: http://apiservice.internal.{{ .Env.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN }}
379+
- name: services__apiservice__https__0
380+
value: https://apiservice.internal.{{ .Env.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN }}
377381
tags:
378382
azd-service-name: webfrontend
379383
aspire-resource-name: webfrontend

docs/deployment/manifest-format.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ Publishing the manifest from the default starter template for Aspire produces th
111111
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
112112
"ConnectionStrings__cache": "{cache.connectionString}",
113113
"APISERVICE_HTTP": "{apiservice.bindings.http.url}",
114-
"APISERVICE_HTTPS": "{apiservice.bindings.https.url}"
114+
"APISERVICE_HTTPS": "{apiservice.bindings.https.url}",
115+
"services__apiservice__http__0": "{apiservice.bindings.http.url}",
116+
"services__apiservice__https__0": "{apiservice.bindings.https.url}"
115117
},
116118
"bindings": {
117119
"http": {
@@ -143,7 +145,9 @@ This dependency is known because the environment variables for the _webfrontend_
143145
// ... other environment variables omitted for clarity
144146
"ConnectionStrings__cache": "{cache.connectionString}",
145147
"APISERVICE_HTTP": "{apiservice.bindings.http.url}",
146-
"APISERVICE_HTTPS": "{apiservice.bindings.https.url}"
148+
"APISERVICE_HTTPS": "{apiservice.bindings.https.url}",
149+
"services__apiservice__http__0": "{apiservice.bindings.http.url}",
150+
"services__apiservice__https__0": "{apiservice.bindings.https.url}"
147151
},
148152
```
149153

docs/extensibility/dev-tunnels-integration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ When another resource references a dev tunnel, environment variables are injecte
102102

103103
```env
104104
WEB_HTTPS=https://myweb-1234.westeurope.devtunnels.ms/
105+
services__web__https__0=https://myweb-1234.westeurope.devtunnels.ms/
105106
```
106107

107108
This lets downstream resources use the tunneled address exactly like any other Aspire service discovery entry.

docs/fundamentals/app-host-overview.md

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,16 @@ Project-to-project references are handled differently than resources that have w
217217
| Method | Environment variable |
218218
|--|--|
219219
| `WithReference(cache)` | `ConnectionStrings__cache="localhost:62354"` |
220-
| `WithReference(apiservice)` | `APISERVICE_HTTP="http://localhost:5455"` <br /> `APISERVICE_HTTPS="https://localhost:7356"` |
220+
| `WithReference(apiservice)` | `APISERVICE_HTTP="http://localhost:5455"` <br /> `APISERVICE_HTTPS="https://localhost:7356"` <br /> `services__apiservice__http__0="http://localhost:5455"` <br /> `services__apiservice__https__0="https://localhost:7356"` |
221221

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. 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).
223230

224231
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:
225232

@@ -243,31 +250,68 @@ var apiservice = builder.AddProject<Projects.AspireApp_ApiService>("apiservice")
243250

244251
| Method | Environment variable |
245252
|---------------------------|-------------------------------------------------------|
246-
| `WithReference(endpoint)` | `MYAPP_ENDPOINT=https://localhost:9043` |
253+
| `WithReference(endpoint)` | `MYAPP_ENDPOINT="https://localhost:9043"` <br /> `services__myapp__endpoint__0="https://localhost:9043"` |
247254

248255
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).
249256

250257
### Service endpoint environment variable format
251258

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:
253262

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)
255264

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.
257266

258267
Consider the following environment variable examples:
259268

260269
```Environment
261270
APISERVICE_HTTP
271+
APISERVICE_HTTPS
262272
```
263273

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:
265275

266276
```Environment
267277
APISERVICE_MYENDPOINT
268278
```
269279

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*>:
307+
308+
```csharp
309+
var projectA = builder.AddProject<Projects.ProjectA>("projecta");
310+
var projectB = builder.AddProject<Projects.ProjectB>("projectb")
311+
.WithEnvironment("PROJECTA_URL", projectA.GetEndpoint("https"));
312+
```
313+
314+
This generates a single environment variable `PROJECTA_URL` with the HTTPS endpoint URL of the `projecta` service.
271315

272316
## Reference existing resources
273317

docs/fundamentals/orchestrate-resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ var frontend = builder.AddProject<Projects.Frontend>("frontend")
285285
.WithReference(api);
286286
```
287287

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.
289289

290290
### External service lifecycle
291291

0 commit comments

Comments
 (0)