Перейти до вмісту

Dapr integration

Цей контент ще не доступний вашою мовою.

⭐ Community Toolkit Dapr logo

Dapr (Distributed Application Runtime) is a portable, event-driven runtime that makes it easy for developers to build resilient, microservice stateless and stateful applications. The Aspire Dapr integration enables you to add Dapr sidecars to your Aspire projects.

To get started with the Aspire Dapr hosting integration, install the CommunityToolkit.Aspire.Hosting.Dapr NuGet package in the app host project.

Aspire CLI — Додати пакет CommunityToolkit.Aspire.Hosting.Dapr
aspire add communitytoolkit-dapr

Aspire CLI інтерактивний; оберіть відповідний результат пошуку:

Aspire CLI — Приклад виводу
Select an integration to add:
> communitytoolkit-dapr (CommunityToolkit.Aspire.Hosting.Dapr)
> Other results listed as selectable options...

In your app host project, add a Dapr sidecar to a project using the WithDaprSidecar extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.ExampleProject>("exampleproject")
.WithDaprSidecar();
// After adding all resources, run the app...

By default, the Dapr sidecar uses the resource name as the Dapr app ID. You can customize the app ID and sidecar options:

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
});
// After adding all resources, run the app...

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 based on the project name and includes the configured ports.

To connect to services with Dapr sidecars from your client application, use the Dapr.AspNetCore or Dapr.Client NuGet packages:

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

For non-ASP.NET Core applications, use the Dapr.Client package instead:

Terminal window
dotnet add package Dapr.Client

In the Program.cs file of your client-consuming project, call the AddDaprClient extension method to register the Dapr client:

builder.Services.AddDaprClient();

You can then retrieve the DaprClient instance using dependency injection:

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. When you add a Dapr sidecar to a project, you can invoke methods on that service using the app ID:

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 on Dapr service invocation, see Dapr service invocation.

Запитання & ВідповідіСпівпрацяСпільнотаОбговоритиПереглянути