using Aspire.Hosting.Lifecycle; using CommunityToolkit.Aspire.Hosting.Dapr; var builder = DistributedApplication.CreateBuilder(args); builder.AddDapr(); builder.Services.AddLifecycleHook(); var cache = builder.AddRedis("cache"); var apiService = builder.AddProject("apiservice") .WithHttpHealthCheck("/health") .WithDaprSidecar(); builder.AddProject("webfrontend") .WithExternalHttpEndpoints() .WithHttpHealthCheck("/health") .WithReference(cache) .WaitFor(cache) .WithReference(apiService) .WaitFor(apiService) .WithDaprSidecar(); builder.Build().Run(); class FlowDaprSidecarAnnotations : IDistributedApplicationLifecycleHook { public Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default) { // We need to find // The dapr sidecar resource // The associated dapr sidecar CLI resource // The original resource that the sidecar is associated with // And then copy the wait annotations from the original resource to the sidecar CLI resource. var resourceMap = appModel.Resources .ToDictionary(r => r.Name, r => r); foreach (var resource in appModel.Resources) { if (resource.TryGetLastAnnotation(out var sidecarAnnotation)) { var sideCar = sidecarAnnotation.Sidecar; var daprCliName = sidecarAnnotation.Sidecar.Name + "-cli"; if (resourceMap.TryGetValue(daprCliName, out var daprCliResource)) { if (resource.TryGetAnnotationsOfType(out var waitAnnotations)) { // Copy annotations from the original resource to the CLI resource foreach (var annotation in waitAnnotations) { daprCliResource.Annotations.Add(annotation); } } if (sideCar.TryGetAnnotationsOfType(out var sidecarWaitAnnotations)) { // Also copy annotations from the sidecar to the CLI resource foreach (var annotation in sidecarWaitAnnotations) { daprCliResource.Annotations.Add(annotation); } } } } } return Task.CompletedTask; } }