Dapr framework integration
Bu içerik henüz dilinizde mevcut değil.
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.
Hosting integration
Section titled “Hosting integration”aspire add communitytoolkit-daprAspire CLI etkileşimlidir; istendiğinde uygun sonucu seçin:
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”Add a Dapr sidecar to any project resource by calling WithDaprSidecar:
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:
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 Dapr state store
Section titled “Add Dapr state store”Add a Dapr state store component with AddDaprStateStore and reference it from the sidecar:
var builder = DistributedApplication.CreateBuilder(args);
var stateStore = builder.AddDaprStateStore("statestore");
builder.AddProject<Projects.ExampleProject>("exampleproject") .WithDaprSidecar(sidecar => sidecar .WithReference(stateStore));
builder.Build().Run();Add Dapr pub/sub
Section titled “Add Dapr pub/sub”Add a Dapr pub/sub component with AddDaprPubSub and reference it from the sidecar:
var builder = DistributedApplication.CreateBuilder(args);
var pubSub = builder.AddDaprPubSub("pubsub");
builder.AddProject<Projects.ExampleProject>("exampleproject") .WithDaprSidecar(sidecar => sidecar .WithReference(pubSub));
builder.Build().Run();Add Dapr component
Section titled “Add Dapr component”Use AddDaprComponent to add any Dapr component by type:
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();Configure an actor state store
Section titled “Configure an actor state store”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:
apiVersion: dapr.io/v1alpha1kind: Componentmetadata: name: statestorespec: 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:
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.ActorService>("actorservice") .WithDaprSidecar(new DaprSidecarOptions { ResourcesPaths = ["./components"] });
builder.Build().Run();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 derived from the project name and includes the configured ports.
Client integration
Section titled “Client integration”To connect from a consuming project to services that have Dapr sidecars, install the 📦 Dapr.AspNetCore package:
dotnet add package Dapr.AspNetCore#:package Dapr.AspNetCore@*<PackageReference Include="Dapr.AspNetCore" Version="*" />For non-ASP.NET Core applications, install 📦 Dapr.Client instead:
dotnet add package Dapr.ClientUse the Dapr client
Section titled “Use the Dapr client”In the Program.cs file of your consuming project, register the Dapr client with the dependency injection container:
builder.Services.AddDaprClient();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. The following example invokes an endpoint on the service identified by the Dapr app ID exampleproject:
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.
Deploy with Azure Container Apps
Section titled “Deploy with Azure Container Apps”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:
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.