Zum Inhalt springen
DokumentationAspire ausprobieren
DokumentationAusprobieren

Set up Dapr resources in the AppHost

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

⭐ Community Toolkit Dapr logo

This article is the AppHost API reference for the 📦 CommunityToolkit.Aspire.Hosting.Dapr package. If you’re new to the integration, start with Get started with Dapr and Aspire. For app-side endpoint variables and SDK examples, see Connect Aspire apps to Dapr sidecars.

Terminal
aspire add CommunityToolkit.Aspire.Hosting.Dapr

This adds the package to aspire.config.json. After aspire restore, .aspire/modules/aspire.mjs includes the Dapr APIs.

Learn more about aspire add and aspire restore.

Add Dapr, create state-store and pub/sub component resources, then reference the components from a sidecar:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
await builder.addDapr();
const stateStore = builder.addDaprStateStore('statestore');
const pubSub = builder.addDaprPubSub('pubsub');
await builder
.addContainer('api', 'nginx')
.withEndpoint({ name: 'http', scheme: 'http', targetPort: 80 })
.configureDaprSidecar(async (sidecar) => {
await sidecar.withOptions({
appId: 'api',
appPort: 80,
appProtocol: 'http',
});
await sidecar.withReference(await stateStore);
await sidecar.withReference(await pubSub);
});
await builder.build().run();

AddDaprStateStore and AddDaprPubSub create generic component resources. Use the generic component API when you need another Dapr component type:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const binding = builder.addDaprComponent('outbound', 'bindings.http');
await builder.build().run();

Use DaprSidecarOptions or its TypeScript equivalent to configure the app ID, app endpoint, Dapr endpoints, health probes, logging, metrics, component paths, and other dapr run settings:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
await builder
.addContainer('api', 'nginx')
.withEndpoint({ name: 'http', scheme: 'http', targetPort: 80 })
.withDaprSidecar({
sidecarOptions: {
appId: 'catalog-api',
appPort: 80,
appProtocol: 'http',
enableApiLogging: true,
resourcesPaths: ['./components'],
},
});
await builder.build().run();

Use WithMetadata or withMetadata for static values. The hosting integration also exposes overloads for endpoint references, reference expressions, and parameters so metadata can follow Aspire-managed resources.

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const redis = builder
.addContainer('redis', 'redis')
.withEndpoint({ name: 'tcp', targetPort: 6379 });
const redisEndpoint = await redis.getEndpoint('tcp');
const redisPassword = await builder.addParameter('redis-password', {
secret: true,
});
const stateStore = builder
.addDaprStateStore('statestore')
.withMetadata('actorStateStore', 'true')
.withMetadataEndpoint('redisHost', redisEndpoint)
.withMetadataParameter('redisPassword', redisPassword);
await builder.build().run();

Each Dapr sidecar is a hidden resource named from its app resource, such as api-dapr. The Aspire dashboard displays the sidecar’s state, endpoints, logs, traces, and metrics with the rest of the application model.

Install 📦 CommunityToolkit.Aspire.Hosting.Azure.Dapr, then enable Dapr components on the Azure Container Apps environment:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const environment = await builder.addAzureContainerAppEnvironment('cae');
await environment.withDaprComponents();
await builder.build().run();

The integration translates local Dapr component and sidecar settings into Azure Container Apps Dapr configuration during publish.