Azure Functions in .NET (C#) — The Ultimate Fun Version

Zied Rebhi
6 min readNov 13, 2024

--

Azure Functions in C#: The Ultimate Fun Version

What is Azure Functions?

Imagine you’re throwing a party. You have tons of guests (events) arriving at different times. Instead of hiring a full-time butler (server) to manage it all, you have Azure Functions, which is like a party planner who only shows up when needed. When a guest arrives (an event happens), the planner (your function) gets to work, does their thing, and leaves when the job is done. No hassle, no drama. Just pure serverless fun!

Why Should You Care About Azure Functions?

  • Zero Server Drama: No need to manage servers. It just works.
  • Scalable Like a Superhero: It grows or shrinks as needed (no stress about overworking it).
  • Pay Only for What You Use: You don’t pay for a full-time worker (server); you only pay for when your party planner shows up.

Setting Up Your First Azure Function

  1. Install Your Tools: Visual Studio or Visual Studio Code is your playground. Make sure to install the Azure Functions Tools. It’s like installing your party setup kit.
  2. Create a New Project: Open Visual Studio and create a new Azure Function project. Choose the HTTP Trigger template (because, hey, you want to start with something easy like handling guest RSVPs via HTTP, right?).

Writing Your First Function (The HTTP Trigger)

Here’s where the magic happens. Imagine this function is like sending an invite that says “Hey, tell me your name!”

Azure function : HTTP Trigger

This function is triggered by an HTTP request. If someone says their name (via the query string name), the function greets them like a true host. If not, they get a “Please tell us your name!” nudge.

Testing It Out Locally:

  • Press F5 and your party planner (Azure Function) will run on your local machine.
  • Open up your browser and go to http://localhost:7071/api/HelloFunction?name=John. Your function should greet you: "Hello, John, welcome to the party!"

Example 2: Timer Trigger (The Office Alarm)

Now, let’s add a fun twist! Imagine you want to send out an announcement every 5 minutes: “Time for snacks!”

Here’s how to set up a Timer Trigger:

Time Trigger with Azure Functions

The Timer Trigger runs every 5 minutes. You can think of it as the office alarm that reminds you to take a break. No more forgetting about snacks!

Example 3: Blob Trigger (Snack Delivery)

What if someone drops a snack (a file) into the Azure Blob Storage, and you want your function to grab it and send an alert? Here’s how:

Blog Trigger

This function gets triggered when a new snack (file) is uploaded to the snack-container. Your function immediately tells everyone, “A new snack just arrived!” 🥳

Example 4: Queue Trigger (Serving Requests)

Sometimes, you might get too many requests at once, so you need a queue. This example shows how you can use a Queue Trigger to process snack requests:

Queue trigger

The function gets triggered whenever a message is added to the snack-requests queue. Imagine a request like “Bring me chips!” 🎉

Example 5: Send an Email (The Party Invite)

Let’s say you want your function to send an email whenever a new user registers for your party (or app, or whatever your event is). You can use a Queue Trigger or HTTP Trigger to handle this.

Here’s a simple example using the SendGrid email service to send an email.

  1. Install SendGrid NuGet Package: First, install the SendGrid NuGet package via NuGet Package Manager or by running this in your terminal: dotnet add package SendGrid
  2. Create the Azure Function to Send an Email:
  • How it works: This function triggers when a new user is added to the new-user-queue. It sends them a welcome email using SendGrid.
  • Real use case: You could use this for sending onboarding emails to new users or subscribers.

Example 6: Process Files in Blob Storage (The Snack Delivery)

Imagine you’re hosting a party and guests are dropping off snack recipes (files) in your Azure Blob Storage. You want to process those recipes and log them in the event that someone needs to order the ingredients.

Processing files in Blob Storage
  • How it works: This function listens to a blob container (snacks/). When a new file is uploaded (for example, a recipe), it triggers and logs the file contents.
  • Real use case: You could use this for handling uploaded documents, images, or reports.

Example 7: Process Messages from Service Bus Queue (The Help Desk)

Let’s say you want your function to act as a help desk that listens for incoming support requests and processes them one by one from the Azure Service Bus queue.

Process Messages from Service Bus Queue
  • How it works: This function is triggered by messages added to the support-requests queue. When a new support request comes in, the function processes it.
  • Real use case: This could be part of a customer service platform where requests are added to a queue and processed sequentially.

Example 8: Durable Functions (The Party Workflow)

What if you have a complex workflow? Maybe you need to send multiple emails, update databases, and even call external APIs as part of a longer-running process (like processing party RSVPs). You can use Durable Functions to chain tasks together and ensure everything runs in order.

  • How it works: This example demonstrates Durable Functions, where multiple activities (like sending an invite, reminding guests about snacks, and confirming RSVPs) are orchestrated together. This is great for workflows that need to maintain state or wait for external inputs.
  • Real use case: Perfect for processing complex tasks like order processing, multi-step approvals, or handling multi-step customer interactions.

Example 9: Integrating with External APIs (The Party Food Order)

What if you need to order food or supplies from an external service (like an API)? Here’s how you can integrate Azure Functions with an external API to automate ordering party supplies!

Integrating with External APIs
  • How it works: This function is triggered daily at noon (with a Timer Trigger). It calls an external API to order pizza for the party (or other supplies).
  • Real use case: You could use this in an e-commerce setting or any business that needs to interact with external APIs on a schedule.

Cost Management: Pay Only for What You Eat (Or Use)

The best part? You only pay when your function is doing its job, like a party planner that only bills you for the time they actually worked! There are several pricing plans:

  • Consumption Plan: Pay for only the time your function runs.
  • Premium Plan: More control, more features (like VNET integration).
  • Dedicated Plan: Full-time party planner with fixed scaling.

Conclusion: More Ways to Party with Azure Functions

With these real examples, you can see how Azure Functions can be used for a variety of tasks — from sending emails, processing files, and interacting with external APIs, to managing complex workflows with Durable Functions.

The best part? Azure Functions is like the best party planner: it shows up when needed, gets the job done, and leaves without any fuss. Plus, it scales automatically, so no party is too big or too small! 🥳

--

--

Zied Rebhi
Zied Rebhi

No responses yet