İçeriğe geç
DocsTry Aspire
DocsTry

Connect Aspire apps to Dapr sidecars

Bu içerik henüz dilinizde mevcut değil.

⭐ Community Toolkit Dapr logo

This page describes how an app connects to the Dapr sidecar attached to it. For sidecar and component configuration in the AppHost, see Set up Dapr resources in the AppHost.

The hosting integration injects these environment variables into an app resource that has a Dapr sidecar:

Environment variableDescription
DAPR_HTTP_PORTPort for the sidecar’s HTTP API
DAPR_GRPC_PORTPort for the sidecar’s gRPC API
DAPR_HTTP_ENDPOINTFull endpoint for the sidecar’s HTTP API
DAPR_GRPC_ENDPOINTFull endpoint for the sidecar’s gRPC API

These variables are added in run mode. Pick the HTTP or gRPC endpoint that matches the SDK transport you configure.

Each example uses the sidecar endpoint injected into the app resource.

Install the official 📦 Dapr.Client package:

.NET CLI — Add Dapr.Client package
dotnet add package Dapr.Client
Program.cs
using Dapr.Client;
var endpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT")
?? throw new InvalidOperationException("DAPR_GRPC_ENDPOINT is not set.");
using var daprClient = new DaprClientBuilder()
.UseGrpcEndpoint(endpoint)
.Build();
var product = await daprClient.GetStateAsync<string>(
"statestore",
"product-1");

ASP.NET Core apps can instead install 📦 Dapr.AspNetCore and register DaprClient with dependency injection.