Lessons in this course (14)

4. Azure Functions (serverless)

Intermediate★ Must knowLesson 4 of 14

Event-driven code that runs on triggers with declarative bindings (input/output), scaling automatically.

Hosting planNotes
Flex Consumption (recommended for new apps)Serverless, scale to zero, fast scale, VNet support, per-execution + memory billing
Consumption (classic)Serverless, cold starts, 5–10 min default/max timeout
Premium (Elastic Premium)Pre-warmed instances (no cold start), VNet, longer runs
Dedicated (App Service plan)Run on existing plan, predictable cost
Container AppsFunctions in containers

.NET uses the isolated worker model (the in-process model ends support on 10 November 2026).

csharp
public class OrderFunctions(ILogger<OrderFunctions> log)
{
    [Function("ProcessOrder")]
    [BlobOutput("receipts/{rand-guid}.json", Connection = "Storage")]
    public string Run([ServiceBusTrigger("orders", Connection = "ServiceBus")] OrderPlaced msg)
    {
        log.LogInformation("Processing order {OrderId}", msg.OrderId);
        return JsonSerializer.Serialize(new Receipt(msg.OrderId, DateTime.UtcNow));
    }

    [Function("Health")]
    public IActionResult Health([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req) => new OkObjectResult("ok");

    [Function("NightlyCleanup")]
    public Task Cleanup([TimerTrigger("0 0 2 * * *")] TimerInfo timer) => Task.CompletedTask;   // 6-field NCRONTAB (with seconds)
}

Triggers: HTTP, Timer, Queue Storage, Blob, Service Bus, Event Grid, Event Hubs, Cosmos DB change feed, SQL (change tracking), Kafka, RabbitMQ. Durable Functions add stateful orchestrations: function chaining, fan-out/fan-in, async HTTP APIs, monitors, human interaction (approval with timeout), entities.