💡 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:
-
New Project → Azure Functions → Next.
-
Name it, choose .NET 6/7/8 (Isolated or In-Process).
-
Choose Trigger type = HTTP Trigger.
-
Authorization level: Anonymous (for testing).
This generates a default function file like:
3️⃣ Run Locally
-
Press F5 (Visual Studio) or use CLI:
-
Test in browser:
http://localhost:7071/api/HelloFunction?name=Shivan
4️⃣ Deploy to Azure
-
Right-click project → Publish → Azure.
-
Choose an existing or new Function App in your Azure subscription.
-
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:
🔌 Input and Output Bindings
Bindings connect your function to other services with minimal code.
Example: Save an HTTP request body to Cosmos DB
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:
-
Create a Function project,
-
Write the function logic,
-
Run locally and deploy.
This makes it perfect for microservices, scheduled jobs, or quick API endpoints without managing servers.
No comments:
Post a Comment