Dapr integration
このコンテンツはまだ日本語訳がありません。
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.
Hosting integration
Section titled “Hosting integration”To get started with the Aspire Dapr hosting integration, install the CommunityToolkit.Aspire.Hosting.Dapr NuGet package in the app host project.
aspire add communitytoolkit-daprAspire CLI は対話的です。求められたら適切な結果を選択してください:
Select an integration to add:
> communitytoolkit-dapr (CommunityToolkit.Aspire.Hosting.Dapr)> Other results listed as selectable options...#:package CommunityToolkit.Aspire.Hosting.Dapr@*<PackageReference Include="CommunityToolkit.Aspire.Hosting.Dapr" Version="*" />Add Dapr sidecar
Section titled “Add Dapr sidecar”In your app host project, add a Dapr sidecar to a project using the WithDaprSidecar extension method:
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:
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...Dashboard integration
Section titled “Dashboard integration”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.
Client integration
Section titled “Client integration”To connect to services with Dapr sidecars from your client application, use the Dapr.AspNetCore or Dapr.Client NuGet packages:
dotnet add package Dapr.AspNetCore#:package Dapr.AspNetCore@*<PackageReference Include="Dapr.AspNetCore" Version="*" />For non-ASP.NET Core applications, use the Dapr.Client package instead:
dotnet add package Dapr.ClientUse Dapr client
Section titled “Use 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"); }}Service-to-service invocation
Section titled “Service-to-service invocation”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.