Building Observable Pub/Sub Microservices with .NET 8 Minimal APIs and Dapr on Azure Container Apps

Mahmut Sarıkaya 3 dk okuma 6 Görüntülenme 0
Building Observable Pub/Sub Microservices with .NET 8 Minimal APIs and Dapr on Azure Container Apps

Why observable Pub/Sub matters in modern cloud apps

Enterprises are moving from monoliths to event‑driven microservices at a record pace; a 2023 Cloud Native Survey showed that 71% of organizations plan to adopt Pub/Sub patterns within the next year. The challenge is not just to publish messages, but to make the flow observable, debuggable, and resilient without writing boiler‑plate infrastructure code.

.NET 8 Minimal APIs as a lightweight entry point

.NET 8 introduced a trimmed‑down programming model that eliminates the ceremony of controllers, Startup classes, and explicit routing tables. A Minimal API can be expressed in fewer than 20 lines, which reduces cognitive load and speeds up iteration cycles. Because the runtime is still the full .NET 8 stack, you keep access to powerful features such as source generators, AOT compilation, and native support for OpenAPI generation.

Introducing Dapr to abstract Pub/Sub mechanics

Dapr (Distributed Application Runtime) acts as a sidecar that implements common building blocks—state stores, bindings, and Pub/Sub—through a language‑agnostic HTTP or gRPC API. By declaring a Pub/Sub component (for example, Azure Service Bus) in a YAML file, the same Minimal API code can switch clouds or brokers simply by swapping the component definition. Dapr also emits tracing, metrics, and logs that integrate with Azure Monitor out of the box.

Deploying to Azure Container Apps

Azure Container Apps (ACA) is a serverless container platform that automatically scales to zero and supports Dapr natively. When you push a Docker image that contains the .NET 8 Minimal API, ACA spins up the sidecar, injects the Pub/Sub component, and configures ingress. The entire stack can be provisioned with Azure CLI in under five minutes, making it ideal for proof‑of‑concepts and production workloads alike.

Step‑by‑step code sample

The following example demonstrates a simple order service that publishes an "order.created" event. The code uses the DaprClient extension, maps a POST endpoint, and annotates the route with .WithTopic so that Dapr can subscribe the same endpoint when the service starts.

using Microsoft.AspNetCore.Builder;<br/>using Microsoft.AspNetCore.Http;<br/>using Dapr.Client;<br/>var builder = WebApplication.CreateBuilder(args);<br/>builder.Services.AddDaprClient();<br/>var app = builder.Build();<br/>app.MapPost("/orders", async (Order order, DaprClient dapr) => {<br/>    await dapr.PublishEventAsync("pubsub", "order.created", order);<br/>    return Results.Accepted();<br/>}).WithTopic("pubsub", "order.created");<br/>app.Run();<br/>public record Order(string Id, decimal Amount, string CustomerId);

Build the container image with the standard .NET SDK commands:

dotnet publish -c Release -o out /p:PublishTrimmed=true<br/>docker build -t myregistry.azurecr.io/orderservice:latest .

Deploy to ACA using the Azure CLI:

az containerapp create \<br/>  --name orderservice \<br/>  --resource-group MyRg \<br/>  --environment MyEnv \<br/>  --image myregistry.azurecr.io/orderservice:latest \<br/>  --dapr-enabled true \<br/>  --dapr-app-id orderservice \<br/>  --ingress external \<br/>  --target-port 80

After deployment, you can test the endpoint with curl or Postman. Dapr will forward the message to the configured Azure Service Bus topic, and any other service that subscribed to "order.created" will receive it instantly.

Monitoring and observability best practices

Because Dapr emits OpenTelemetry traces, enable Azure Monitor integration by setting the AZURE_MONITOR_ENABLED environment variable in the Container App. Combine this with .NET 8 built‑in logging (ILogger) to correlate HTTP request IDs with Pub/Sub events. A practical tip: add a correlation ID header (e.g., X‑Correlation‑Id) in the Minimal API and propagate it through DaprClient using PublishEventAsync overloads that accept Dictionary<string,string> metadata.

Conclusion

By marrying .NET 8 Minimal APIs, Dapr’s Pub/Sub abstraction, and Azure Container Apps’ serverless execution model, you can build observable microservices in a fraction of the time required by traditional frameworks. The approach eliminates infrastructure lock‑in, provides out‑of‑the‑box tracing, and scales automatically, allowing developers to focus on business logic rather than plumbing.

Sources

  • Microsoft Docs – .NET 8 Minimal APIs
  • Dapr Documentation – Pub/Sub Building Block
  • Azure Docs – Azure Container Apps with Dapr

Author: Mahmut Sarıkaya — sarikayadev.com

Etiketler: #.NET 8 #Minimal APIs #Dapr #Azure Container Apps #Pub/Sub
Paylaş:
M

Yazar

Mahmut Sarıkaya

yazılım Geliştirici

Yorumlar

Henüz yorum yok. İlk yorumu siz yapın!

Yorum Bırakın

0 + 4 =