Monday, 22 September 2025

Azure Function

💡 What is an Azure Function?

Azure Functions is a serverless compute service.

  • You write small pieces of code (“functions”) that run on demand.

  • Microsoft manages all the servers, scaling, and infrastructure.

  • You pay only for the execution time.


🚀 Key Features

  • Event-driven: Triggered by events like HTTP requests, a queue message, a timer, or a blob upload.

  • Automatic scaling: Runs as many instances as needed.

  • Multiple triggers & bindings: Connect to storage, Service Bus, Cosmos DB, etc.


🏗️ Create a Simple HTTP-Triggered Function (C#)

1️⃣ Prerequisites

  • .NET 6/7/8 SDK (latest recommended)

  • Azure Functions Core Tools

  • Visual Studio 2022 or VS Code with Azure Functions extension

  • Azure subscription (for deployment)


2️⃣ Create the Project

With Visual Studio:

  1. New Project → Azure Functions → Next.

  2. Name it, choose .NET 6/7/8 (Isolated or In-Process).

  3. Choose Trigger type = HTTP Trigger.

  4. Authorization level: Anonymous (for testing).

This generates a default function file like:

using System.Net; public static class HelloFunction { [FunctionName("HelloFunction")] public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string name = req.Query["name"]; return new OkObjectResult($"Hello, {name ?? "world"}!"); } }

3️⃣ Run Locally

  • Press F5 (Visual Studio) or use CLI:

    func start
  • Test in browser:
    http://localhost:7071/api/HelloFunction?name=Shivan


4️⃣ Deploy to Azure

  1. Right-click project → Publish → Azure.

  2. Choose an existing or new Function App in your Azure subscription.

  3. After deployment, Azure gives you a public URL.


⏱️ Other Common Triggers

  • Timer Trigger: Run on a schedule (like a cron job).

  • Queue/Service Bus Trigger: Run when a message arrives.

  • Blob Trigger: Run when a file is uploaded to Azure Storage.

Example Timer Trigger:

[FunctionName("DailyCleanup")] public static void Run([TimerTrigger("0 0 2 * * *")]TimerInfo myTimer, ILogger log) { log.LogInformation($"Cleanup executed at: {DateTime.Now}"); }

🔌 Input and Output Bindings

Bindings connect your function to other services with minimal code.

Example: Save an HTTP request body to Cosmos DB

[FunctionName("SaveOrder")] public static async Task<IActionResult> Run( [HttpTrigger("post")] HttpRequest req, [CosmosDB( databaseName: "OrdersDb", containerName: "Orders", Connection = "CosmosDBConnection")] IAsyncCollector<Order> ordersOut, ILogger log) { var order = await JsonSerializer.DeserializeAsync<Order>(req.Body); await ordersOut.AddAsync(order); return new OkResult(); }

No explicit Cosmos client code required—Azure handles it.


⚙️ Hosting Plans

  • Consumption (default): Pay per execution, scales automatically.

  • Premium: Always warm, more performance.

  • Dedicated (App Service Plan): Fixed compute, for special cases.


✅ Key Points

  • Event-driven & serverless → write only the logic.

  • Supports many triggers/bindings (HTTP, Timer, Queue, Blob, Event Grid, etc.).

  • Write in C# (in-process or isolated process) with .NET 6/7/8.

  • Local dev → test → deploy to Azure with one command.


🏁 Summary

An Azure Function in C# is just a small method decorated with a [FunctionName] attribute that responds to a trigger (HTTP, Timer, Queue, etc.).
You:

  1. Create a Function project,

  2. Write the function logic,

  3. Run locally and deploy.

This makes it perfect for microservices, scheduled jobs, or quick API endpoints without managing servers.

No comments:

Post a Comment