Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Created August 6, 2025 15:03
Show Gist options
  • Save davidfowl/86a7c0fd3e419f5714d0c03c8c1bca3c to your computer and use it in GitHub Desktop.
Save davidfowl/86a7c0fd3e419f5714d0c03c8c1bca3c to your computer and use it in GitHub Desktop.
using Aspire.Hosting.Lifecycle;
using CommunityToolkit.Aspire.Hosting.Dapr;
var builder = DistributedApplication.CreateBuilder(args);
builder.AddDapr();
builder.Services.AddLifecycleHook<FlowDaprSidecarAnnotations>();
var cache = builder.AddRedis("cache");
var apiService = builder.AddProject<Projects.daprapp_ApiService>("apiservice")
.WithHttpHealthCheck("/health")
.WithDaprSidecar();
builder.AddProject<Projects.daprapp_Web>("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<DaprSidecarAnnotation>(out var sidecarAnnotation))
{
var sideCar = sidecarAnnotation.Sidecar;
var daprCliName = sidecarAnnotation.Sidecar.Name + "-cli";
if (resourceMap.TryGetValue(daprCliName, out var daprCliResource))
{
if (resource.TryGetAnnotationsOfType<WaitAnnotation>(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<WaitAnnotation>(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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment