Cloudflare Queues
Cloudflare Queues is a message queue. The integration provisions a queue and gives you an IQueueClient to send and pull messages from .NET (using the HTTP push producer and pull consumer).
Add a queue (hosting)
Section titled “Add a queue (hosting)”builder.AddCloudflareEnvironment();var jobs = builder.AddQueue("jobs").RunAsEmulator();await builder.addCloudflareEnvironment();const jobs = await builder.addQueue('jobs').runAsEmulator();aspire run→ with.RunAsEmulator(), an in-process in-memory queue backs it (with lease/visibility semantics). No credentials needed.aspire deploy→ the queue is provisioned in real Queues via the Cloudflare API.
Reference it from a service
Section titled “Reference it from a service”builder.AddProject<Projects.Api>("api").WithReference(jobs);await builder.addProject('api', '../Api/Api.csproj').withReference(jobs);Use it (client)
Section titled “Use it (client)”builder.AddQueueClient("jobs");This registers an IQueueClient bound to the queue. Send messages, then pull and acknowledge them:
// Producerapp.MapPost("/enqueue", async (string job, IQueueClient queue, CancellationToken ct) =>{ await queue.SendAsync(job, ct); return Results.Accepted();});
// Consumerapp.MapPost("/process", async (IQueueClient queue, CancellationToken ct) =>{ var messages = await queue.PullAsync(batchSize: 10, cancellationToken: ct); foreach (var message in messages) { // ... handle message.Body ... } await queue.AckAsync(messages.Select(m => m.LeaseId), ct); return Results.Ok(new { processed = messages.Count });});Pulled messages are leased (hidden from other pulls) until you AckAsync (remove) or RetryAsync (return to the queue) them, or the lease expires.
How the backend is chosen
Section titled “How the backend is chosen”Provider |
Backend | When |
|---|---|---|
Local |
In-process in-memory queue | aspire run with .RunAsEmulator() |
Queues |
The Queues HTTP API (push + pull) | Deployed |
Credentials
Section titled “Credentials”- API token: needs Queues: Edit — see API Tokens. The client reuses the environment’s token by default; scope it with
.WithAccessToken(parameter)if you prefer. - Local dev with
.RunAsEmulator(): none.
