-
Notifications
You must be signed in to change notification settings - Fork 72
Adds a new section that describes WithReference and equivalent methods in the "What are Aspire Integrations?" article #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,7 +6,9 @@ lastUpdated: false | |||||
| editUrl: false | ||||||
| --- | ||||||
|
|
||||||
| import { Aside } from '@astrojs/starlight/components'; | ||||||
| import { Aside, Tabs, TabItem } from '@astrojs/starlight/components'; | ||||||
| import PivotSelector from '@components/PivotSelector.astro'; | ||||||
| import Pivot from '@components/Pivot.astro'; | ||||||
|
|
||||||
| Aspire integrations are a curated suite of packages that make it easy to connect your cloud-native applications with popular services like Redis and PostgreSQL. Each integration provides essential cloud-native capabilities through automatic setup or standardized configuration. | ||||||
|
|
||||||
|
|
@@ -43,6 +45,113 @@ These packages configure existing client libraries to connect to hosting integra | |||||
|
|
||||||
| Hosting and client integrations work together but are **not** coupled and can be used independently. Configuration via environment variables connects client integrations to their corresponding hosting integrations, with the AppHost project managing this connection. | ||||||
|
|
||||||
| ## Wiring resources to consuming projects with references | ||||||
|
|
||||||
| Once you've created resources, such as databases or messaging systems, by setting up hosting integrations in the AppHost, you can pass those resources to consuming projects, such as APIs or web apps, by calling the `WithReference` method, or its equivalent for your preferred language. These methods express an explicit dependency between a resource and a consuming project. When Aspire starts, it injects connection information as environment variables into the consuming project so it can locate and connect to that resource. | ||||||
|
|
||||||
| <PivotSelector | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should have a generic mermaid diagram here or nearby, the is similar to what was added in the Postgres get started, that shows two "resources" modeled in the AppHost and helps visualize how hosting and client integrations relate from that perspective. |
||||||
| title="Select your AppHost language" | ||||||
| key="aspire-lang" | ||||||
| options={[ | ||||||
| { id: 'csharp', title: 'C#' }, | ||||||
| { id: 'typescript', title: 'TypeScript' }, | ||||||
| ]} | ||||||
| /> | ||||||
|
Comment on lines
+52
to
+59
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use tabs here, no need for the pivot. You're already using tabs below.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||||||
|
|
||||||
| <Pivot id="csharp"> | ||||||
|
|
||||||
| ```csharp title="C# — AppHost.cs" | ||||||
| var builder = DistributedApplication.CreateBuilder(args); | ||||||
|
|
||||||
| var cache = builder.AddRedis("cache"); | ||||||
| var db = builder.AddPostgres("postgres").AddDatabase("appdb"); | ||||||
|
|
||||||
| builder.AddProject<Projects.Api>("api") | ||||||
| .WithReference(cache) | ||||||
| .WithReference(db); | ||||||
|
|
||||||
| builder.Build().Run(); | ||||||
| ``` | ||||||
|
|
||||||
| </Pivot> | ||||||
|
|
||||||
| <Pivot id="typescript"> | ||||||
|
|
||||||
| ```typescript title="TypeScript — apphost.ts" twoslash | ||||||
| import { createBuilder } from './.modules/aspire.js'; | ||||||
|
|
||||||
| const builder = await createBuilder(); | ||||||
|
|
||||||
| const cache = await builder.addRedis("cache"); | ||||||
| const db = (await builder.addPostgres("postgres")).addDatabase("appdb"); | ||||||
|
|
||||||
| const api = await builder.addProject("api", "../Api/Api.csproj"); | ||||||
| await api.withReference(cache); | ||||||
| await api.withReference(db); | ||||||
|
|
||||||
| await builder.build().run(); | ||||||
| ``` | ||||||
|
|
||||||
| </Pivot> | ||||||
|
|
||||||
| ### Environment variables injected with references | ||||||
|
|
||||||
| The environment variables Aspire injects depend on the type of resource being referenced. For example: | ||||||
|
|
||||||
| - **Connection string resources**: Aspire sets a `ConnectionStrings__{name}` environment variable, where `{name}` matches the resource name. For example, `.WithReference(cache)` on a resource named `"cache"`, that is available on port 6379, injects `ConnectionStrings__cache=localhost:6379`. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to avoid a callout to an explicit .NET API, simply say something like "when one resource chains a call to reference another resource". Leave the code parts for the tabs.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||||||
| - **Service endpoint resources**: Aspire sets `services__{name}__{scheme}__{index}` variables used for service discovery. For example, referencing a project named `api`, that is available on port 7001, injects `services__api__https__0=https://localhost:7001`. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the simple env var names instead of the legacy names.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||||||
|
|
||||||
| Other environment variables often include credentials, port numbers, and other connection details. | ||||||
|
|
||||||
| ### Consuming the injected variables | ||||||
|
|
||||||
| Any application can read these environment variables to connect to its dependencies. A Python, Node.js, or other app can read variables directly, without any Aspire-specific libraries. | ||||||
|
|
||||||
| If your consuming project is written in .NET C#, you have an additional option: [**Aspire client integrations**](#client-integrations): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to avoid saying ".NET", "C#" is much better branding. Also, I think we should just let the tabs speak for themselves. Instead of calling out C# here - instead let the reader select C# if they want those details. In other words, please just fold this into the C# tab, let the reader know they can manually use Environment.GetEnvironmentVariable or the IConfiguration APIs, or preferably use the official client integrations.
Suggested change
|
||||||
|
|
||||||
| <Tabs syncKey="consuming-project-lang"> | ||||||
| <TabItem id="csharp" label="C#"> | ||||||
|
|
||||||
| ```csharp title="C# — Program.cs (consuming .NET project)" | ||||||
| var builder = WebApplication.CreateBuilder(args); | ||||||
|
|
||||||
| // Client integration reads ConnectionStrings__cache injected by WithReference | ||||||
| builder.AddRedisClient(connectionName: "cache"); | ||||||
|
|
||||||
| var app = builder.Build(); | ||||||
| ``` | ||||||
|
|
||||||
| </TabItem> | ||||||
| <TabItem id="typescript" label="TypeScript"> | ||||||
|
|
||||||
| ```typescript title="TypeScript — app.ts" | ||||||
| // Read the injected environment variable directly | ||||||
| const redisUrl = process.env["ConnectionStrings__cache"]; | ||||||
| ``` | ||||||
|
|
||||||
| </TabItem> | ||||||
| <TabItem id="go" label="Go"> | ||||||
|
|
||||||
| ```go title="Go — main.go" | ||||||
| import "os" | ||||||
|
|
||||||
| // Read the injected environment variable directly | ||||||
| redisURL := os.Getenv("ConnectionStrings__cache") | ||||||
| ``` | ||||||
|
|
||||||
| </TabItem> | ||||||
| <TabItem id="python" label="Python"> | ||||||
|
|
||||||
| ```python title="Python — app.py" | ||||||
| import os | ||||||
|
|
||||||
| # Read the injected environment variable directly | ||||||
| redis_url = os.getenv("ConnectionStrings__cache") | ||||||
| ``` | ||||||
|
|
||||||
| </TabItem> | ||||||
| </Tabs> | ||||||
|
|
||||||
| ## Integration features | ||||||
|
|
||||||
| When you add a client integration to a project within your Aspire solution, _service defaults_ are automatically applied to that project; meaning the Service Defaults project is referenced and the `AddServiceDefaults` extension method is called. These defaults are designed to work well in most scenarios and can be customized as needed. The following service defaults are applied: | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - this works well. Fixed!