Stateful Serverless Workflows in .NET 8 with Azure Durable Functions and Minimal APIs

Mahmut Sarıkaya 3 dk okuma 6 Görüntülenme 0
Stateful Serverless Workflows in .NET 8 with Azure Durable Functions and Minimal APIs

Why stateful serverless matters in modern .NET applications

Imagine a retail checkout that must validate inventory, process payment, and send a confirmation email—all without a single server to manage. In 2023, Azure reported that over 40% of new cloud workloads use some form of serverless, yet many developers still struggle to keep state across those short‑lived functions. .NET 8 introduces improvements that make stateful serverless not only possible but also efficient when combined with Azure Durable Functions.

Getting started with .NET 8 Minimal APIs

Minimal APIs let you define HTTP endpoints with just a few lines of code, cutting boilerplate and speeding up prototyping. The following snippet creates a simple GET endpoint that returns a greeting. This is the foundation for triggering more complex orchestrations later.

using Microsoft.AspNetCore.Builder;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/hello", () => "Hello from .NET 8 Minimal API");
app.Run();

Compile with dotnet run and navigate to http://localhost:5000/hello to see the response.

Introducing Azure Durable Functions for workflow orchestration

Durable Functions extend Azure Functions with a built‑in state machine. An orchestrator function can call activity functions, wait for external events, and resume after days, all while Azure handles the persistence. Below is a basic orchestrator that calls two activities: checking stock and charging a payment method.

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using System.Threading.Tasks;

public static class OrderOrchestrator
{
    [FunctionName("OrderOrchestrator")]
    public static async Task Run([
        OrchestrationTrigger] IDurableOrchestrationContext ctx)
    {
        var orderId = ctx.GetInput();
        var stockOk = await ctx.CallActivityAsync<bool>("CheckStock", orderId);
        if (!stockOk) throw new InvalidOperationException("Out of stock");
        var paymentOk = await ctx.CallActivityAsync<bool>("ProcessPayment", orderId);
        if (!paymentOk) throw new InvalidOperationException("Payment failed");
        await ctx.CallActivityAsync("SendConfirmation", orderId);
    }
}

Each activity runs independently, and the orchestrator’s state is stored automatically in Azure Storage.

Combining Minimal APIs and Durable Functions

To expose the workflow as a public endpoint, you can call the orchestrator from a Minimal API route. The API receives an order payload, starts the orchestration, and returns the instance ID so callers can query status later.

app.MapPost("/orders", async (OrderDto order, IDurableClient client) =>
{
    string instanceId = await client.StartNewAsync("OrderOrchestrator", order.OrderId);
    return Results.Accepted($"/orders/status/{instanceId}");
});

app.MapGet("/orders/status/{id}", async (string id, IDurableClient client) =>
{
    var status = await client.GetStatusAsync(id);
    return status is null ? Results.NotFound() : Results.Ok(status);
});

Notice how the Minimal API stays lightweight while the heavy lifting—state management, retries, and durability—is delegated to Durable Functions.

Practical tips for production readiness

1. **Configure storage redundancy**: Use Geo‑redundant storage for the orchestration state to survive regional outages. 2. **Set appropriate timeouts**: Durable Functions default to 30‑day execution; adjust maxDuration in host.json if you need longer windows. 3. **Enable monitoring**: Azure Application Insights automatically captures orchestration steps; add custom events for critical checkpoints. 4. **Version your APIs**: Prefix routes with /v1 or /v2 to avoid breaking clients when you evolve the workflow logic. 5. **Secure the endpoint**: Apply Azure AD authentication to the Minimal API and use managed identities for the Durable Functions to access other Azure services without secrets.

Conclusion

Stateful serverless workflows are no longer a theoretical concept for .NET developers. By leveraging .NET 8 Minimal APIs to expose clean HTTP endpoints and Azure Durable Functions for reliable orchestration, you can build scalable, maintainable, and cost‑effective solutions. The pattern reduces operational overhead, guarantees exactly‑once execution, and keeps your codebase idiomatic to C#.

Sources

  • Microsoft Docs – Azure Durable Functions Overview
  • Microsoft Docs – .NET 8 Minimal APIs
  • Azure Architecture Center – Serverless design patterns

Author: Mahmut Sarıkaya — sarikayadev.com

Etiketler: #dotnet 8 #minimal APIs #azure durable functions #stateful serverless #workflow orchestration
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 + 6 =