When to use Azure Functions instead of Azure Web App Service?
I was unaware of the Azure feature which allows you to publish a Function and then only called on demand. So, I asked the Perplexity AI to give me an example, and I found the answer interesting:
A great candidate for Azure Functions is a small, event‑driven piece of logic that runs only when something happens (HTTP call, timer, queue message, file upload, etc.), instead of a full web app that must stay online 24/7.reviewnprep+2
Below is a C# example of a typical Azure Function that fits that pattern: a background job that processes an order and sends an email when a message arrives in an Azure Service Bus queue. howik+2
Example: Order‑processing Azure Function (C#)
using Azure.Messaging.ServiceBus; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; namespace MyFunctions; public class ProcessOrderFunction { private readonly ILogger<ProcessOrderFunction> _logger; public ProcessOrderFunction(ILogger<ProcessOrderFunction> logger) { _logger = logger; } [Function("ProcessOrder")] public async Task Run( [ServiceBusTrigger("orders-queue", Connection = "ServiceBusConnection")] ServiceBusReceivedMessage message, ServiceBusMessageActions messageActions) { try { var orderId = message.Subject; _logger.LogInformation("Processing order {OrderId}", orderId); // 1. Validate / enrich order (e.g., call DB or another API) await Task.Delay(100); // simulate work // 2. Send confirmation email (or call SendGrid / Azure Communication) await Task.Delay(100); // simulate email send // 3. Complete the message so it doesn't get reprocessed await messageActions.CompleteMessageAsync(message); _logger.LogInformation("Order {OrderId} processed successfully", orderId); } catch (Exception ex) { _logger.LogError(ex, "Failed to process order {OrderId}", message.Subject); // Optionally dead‑letter or abandon the message await messageActions.AbandonMessageAsync(message); } } }
Why this is a good Azure Functions candidate
Event‑driven: Runs only when a message lands in
orders-queue.linkedin+1Short‑lived: Does one focused job (process order + send email), then exits.learn.microsoft+1
Serverless / pay‑per‑use: You don’t pay for idle time; Azure Functions scales automatically when the queue fills.linkedin+2
When to use Azure Functions vs Azure Web App
| Scenario | Better fit |
|---|---|
| Full MVC / Blazor / REST API with routes | Azure Web App |
| Admin UI, SPA, e‑commerce site | Azure Web App |
| Background jobs, timers, queue processing | Azure Functions |
| Lightweight HTTP‑triggered micro‑APIs | Azure Functions |
| Image/file processing on upload | Azure Functions |
The [Function("ProcessOrder")] decorator and the surrounding code are part of the Azure Functions SDK for .NET and they are tightly related.
What [Function("ProcessOrder")] does
It marks a method as an Azure Function entry point.
The string
"ProcessOrder"becomes the function name you see in the Azure portal and in logs.Without this attribute (or the older
[FunctionName("...")]), Azure Functions would not treat that method as a function.learn.microsoft+2
Which library / package you need
For the example I showed (with Microsoft.Azure.Functions.Worker and ILogger<ProcessOrderFunction>), you’re using the .NET isolated worker model for Azure Functions, which comes from:
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="..." /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="..." />
Those packages bring:
The
[Function]attribute.The trigger attributes like
[ServiceBusTrigger].The logging and DI integration for isolated‑process functions.learn.microsoft+1
How this compares to “old‑style” Functions
In the in‑process model (classic Azure Functions class‑library projects), you instead see:
[FunctionName("ProcessOrder")] public static void Run( [ServiceBusTrigger("orders-queue", Connection = "ServiceBusConnection")] string message, ILogger log) { ... }
Here the attribute is called [FunctionName] and lives in Microsoft.Azure.WebJobs‑based packages.learn.microsoft+2
If you tell me which template you’re using in Visual Studio / VS Code (e.g., “Azure Functions – .NET 6 isolated worker”), I can show you the exact csproj dependencies and a minimal HTTP‑trigger example you can run locally with func start.
Comments
Post a Comment