Aller au contenu
Docs Try Aspire
Docs Try

Dapr framework integration

Ce contenu n’est pas encore disponible dans votre langue.

⭐ Community Toolkit Dapr logo

Dapr (Distributed Application Runtime) is a portable, event-driven runtime that makes it easy to build resilient, stateless and stateful microservice applications. The Aspire Dapr integration enables you to add Dapr sidecars to your Aspire project resources and wire in state store, pub/sub, and component resources.

Aspire CLI — Ajouter le package CommunityToolkit.Aspire.Hosting.Dapr
aspire add communitytoolkit-dapr

La CLI Aspire est interactive ; choisissez le résultat approprié lorsque demandé :

Aspire CLI — Exemple de sortie
Select an integration to add:
> communitytoolkit-dapr (CommunityToolkit.Aspire.Hosting.Dapr)
> Other results listed as selectable options...

Add a Dapr sidecar to any project resource by calling WithDaprSidecar:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.ExampleProject>("exampleproject")
.WithDaprSidecar();
builder.Build().Run();

By default, the sidecar uses the resource name as the Dapr app ID. To customize the app ID and ports, pass a DaprSidecarOptions instance:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.ExampleProject>("exampleproject")
.WithDaprSidecar(new DaprSidecarOptions
{
AppId = "my-app-id",
DaprGrpcPort = 50001,
DaprHttpPort = 3500,
MetricsPort = 9090
});
builder.Build().Run();

Add a Dapr state store component with AddDaprStateStore and reference it from the sidecar:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var stateStore = builder.AddDaprStateStore("statestore");
builder.AddProject<Projects.ExampleProject>("exampleproject")
.WithDaprSidecar(sidecar => sidecar
.WithReference(stateStore));
builder.Build().Run();

Add a Dapr pub/sub component with AddDaprPubSub and reference it from the sidecar:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var pubSub = builder.AddDaprPubSub("pubsub");
builder.AddProject<Projects.ExampleProject>("exampleproject")
.WithDaprSidecar(sidecar => sidecar
.WithReference(pubSub));
builder.Build().Run();

Use AddDaprComponent to add any Dapr component by type:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var binding = builder.AddDaprComponent("binding", "bindings.http");
builder.AddProject<Projects.ExampleProject>("exampleproject")
.WithDaprSidecar(sidecar => sidecar
.WithReference(binding));
builder.Build().Run();

To use an actor state store, add the actorStateStore metadata to a component YAML file. Create a components directory in your AppHost and add a component configuration file:

YAML — components/statestore.yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: actorStateStore
# Setting actorStateStore to "true" designates this state store
# as the actor state store. Dapr actors require exactly one state
# store to be configured with this flag.
value: "true"

Reference the components directory from DaprSidecarOptions:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.ActorService>("actorservice")
.WithDaprSidecar(new DaprSidecarOptions
{
ResourcesPaths = ["./components"]
});
builder.Build().Run();

When you add Dapr sidecars to your projects, they appear as separate resources in the Aspire dashboard. Each sidecar is displayed with a unique name derived from the project name and includes the configured ports.

To connect from a consuming project to services that have Dapr sidecars, install the 📦 Dapr.AspNetCore package:

.NET CLI — Add Dapr.AspNetCore package
dotnet add package Dapr.AspNetCore

For non-ASP.NET Core applications, install 📦 Dapr.Client instead:

Terminal
dotnet add package Dapr.Client

In the Program.cs file of your consuming project, register the Dapr client with the dependency injection container:

C# — Program.cs
builder.Services.AddDaprClient();

Retrieve the DaprClient instance using dependency injection:

C# — ExampleService.cs
public class ExampleService(DaprClient daprClient)
{
public async Task CallServiceAsync()
{
var response = await daprClient.InvokeMethodAsync<Response>(
HttpMethod.Get,
"exampleproject",
"api/data");
}
}

Dapr enables service-to-service invocation using the app ID. The following example invokes an endpoint on the service identified by the Dapr app ID exampleproject:

C# — Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDaprClient();
var app = builder.Build();
app.MapGet("/call-service", async (DaprClient daprClient) =>
{
var result = await daprClient.InvokeMethodAsync<string>(
HttpMethod.Get,
"exampleproject",
"api/data");
return Results.Ok(result);
});
app.Run();

For more information, see Dapr service invocation.

The Dapr integration configures Dapr sidecars for local development. When deploying to Azure Container Apps (ACA), Dapr sidecar settings are part of the container app configuration and must be preserved on each deployment.

To ensure Dapr settings are preserved, configure the sidecar explicitly using PublishAsAzureContainerApp:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.ExampleProject>("exampleproject")
.WithDaprSidecar()
.PublishAsAzureContainerApp((infra, app) =>
{
app.Configuration.Dapr = new()
{
IsEnabled = true,
AppId = "exampleproject",
AppPort = 8080,
};
});
builder.Build().Run();

For more information, see Configure Azure Container Apps.