Connect Aspire apps to Data API Builder
Este conteúdo não está disponível em sua língua ainda.
This page describes how consuming apps connect to a Data API Builder resource. For configuration files, database references, image settings, and health checks, see Set up Data API Builder in the AppHost.
DAB has no dedicated Community Toolkit client package. It exposes standard REST and GraphQL endpoints, so use the HTTP or GraphQL client for your app’s language.
Connection values
Section titled “Connection values”DataApiBuilderContainerResource implements service discovery rather than a connection string. Referencing a DAB resource named dab injects:
| Environment variable | Description |
|---|---|
services__dab__http__0 | Resolved URL for the DAB HTTP endpoint |
The AppHost resource also exposes endpoint properties for building custom expressions. These are AppHost properties, not additional consuming-app environment variables. See Use endpoint properties for synchronized C# and TypeScript examples.
Connect from your app
Section titled “Connect from your app”Each example calls the REST route /api/Product. Use /graphql for GraphQL requests.
Apps that use Aspire service defaults can address the resource by name:
builder.Services.AddHttpClient("dab", client =>{ client.BaseAddress = new Uri("https+http://dab");});Or read the resolved service-discovery endpoint directly:
var endpoint = Environment.GetEnvironmentVariable("services__dab__http__0") ?? throw new InvalidOperationException( "services__dab__http__0 is not set.");
using var client = new HttpClient{ BaseAddress = new Uri(endpoint)};
var products = await client.GetStringAsync("/api/Product");Node.js includes fetch, so no DAB-specific package is required:
const endpoint = process.env.services__dab__http__0;if (!endpoint) { throw new Error('services__dab__http__0 is not set.');}
const response = await fetch(new URL('/api/Product', endpoint));if (!response.ok) { throw new Error(`DAB request failed: ${response.status}`);}
const products = await response.json();Use urllib.request from the Python standard library:
import jsonimport osfrom urllib.request import urlopen
endpoint = os.environ["services__dab__http__0"].rstrip("/")
with urlopen(f"{endpoint}/api/Product") as response: products = json.load(response)Use Go’s standard net/http package:
package main
import ( "fmt" "io" "net/http" "os" "strings")
func main() { endpoint := strings.TrimRight( os.Getenv("services__dab__http__0"), "/", ) if endpoint == "" { panic("services__dab__http__0 is not set") }
response, err := http.Get(endpoint + "/api/Product") if err != nil { panic(err) } defer response.Body.Close()
body, err := io.ReadAll(response.Body) if err != nil { panic(err) }
fmt.Println(string(body))}