Getting Started with Azure Functions Using C#

Azure Functions is a serverless compute service provided by Microsoft Azure that allows developers to run event-driven code without having to explicitly manage infrastructure. You only pay for the resources you consume, making it a cost-effective and scalable solution for many workloads.

In this post, we’ll explore what Azure Functions are, why they’re useful,why different from Azure Service App and worker, and walk through how to build a simple Azure Function using C#.

Getting Started with Azure Functions Using C#

What is an Azure Function?

Azure Function is a serverless compute service provided by Microsoft Azure that lets you run small pieces of code (called functions) without worrying about managing servers or infrastructure. In Simple Terms, an Azure Function is like a small app that runs code in response to events, such as:

  • An HTTP request
  • A file uploaded to storage
  • A message in a queue
  • A timer (e.g., every 5 minutes)

It supports multiple languages, including C#, JavaScript, Python, Java, PowerShell, and more.

Benefits of Azure Functions

  • Serverless: No need to manage infrastructure — Azure handles it.
  • Event-driven: Functions are triggered by events like HTTP requests, timers, queues, etc.
  • Auto-scaling: Azure automatically scales your function depending on demand.
  • Pay-per-use: You pay only for the time your code runs (e.g., per execution or GB-s).
  • Multiple Languages: Supports C#, JavaScript, Python, PowerShell, Java, etc.

Common Use Cases:

  • Processing files uploaded to Azure Blob Storage
  • PRunning scheduled background jobs
  • PResponding to HTTP API calls
  • PIntegrating with other Azure services (e.g., Cosmos DB, Event Grid)
  • PSending notifications (email, push, SMS) when events occur

Build Azure Function in C#

To build an Azure Function in C#, you can use either the Azure Portal or locally with Visual Studio / VS Code. Below is a step-by-step guide for both options — starting with Visual Studio, the most common and powerful approach for C# development.

Prerequisites:
  • Visual Studio (2022 or later)
  • Azure Development workload installed
  • An active Azure account

Using Visual Studio
  1. Create a New Project:
    • Open Visual Studio
    • Select "Create a new project"
    • Choose "Azure Functions" as the project template
    • Click Next
  2. Configure the Project:
    • Name your project (e.g., MyFirstFunction)
    • Choose a location and click Create
  3. Choose a Trigger Type:
    • In the next window: Choose the trigger (e.g., HTTP trigger)
    • Choose the Authorization Level (e.g., Anonymous for testing)
    • Set the runtime stack: .NET 6 (LTS) or .NET 8
    • Click Create
  4. Code Your Function
    using System.IO;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Logging;
    using Newtonsoft.Json;
    public static class HelloFunction
    {
      [FunctionName("HelloFunction")]
      public static async Task<IActionResult> Run(
      [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
      ILogger log)
      {
        log.LogInformation("C# HTTP trigger function processed a request.");
        string name = req.Query["name"];
        if (string.IsNullOrEmpty(name))
        {
          return new BadRequestObjectResult("Please pass a name on the query string");
        }
        return new OkObjectResult($"Hello, {name}!");
      }
    }
    
  5. Run Locally
    • Hit F5 or click Start Debugging
    • A console will open showing a local URL like:
      http://localhost:7071/api/HelloFunction
      
    • Open it in a browser to test your function.
  6. Publish to Azure
    • Right-click on the project in Solution Explorer
    • Choose Publish
    • Select or create an Azure Function App
    • Publish the code to Azure
    • Once deployed, you'll get a public URL to call your function.

Build with Azure Portal

This is quick and each process

  1. Go to Azure Portal
  2. Search for Function App and click Create
  3. Fill in details:
    • Subscription, Resource Group
    • App name (unique)
    • Runtime stack: .NET
    • Region
  4. Once created, go to your Function App
  5. In the Functions blade, click Create FunctionChoose HTTP trigger, name it, and click Create
  6. Choose HTTP trigger, name it, and click Create
  7. Edit the C# code in the browser editor
  8. Save and test using the Test/Run tab or public URL

What is an Azure Function App?

An Azure Function App is a container or deployment unit for one or more Azure Functions. It provides the environment in which your functions run.

What does a Function App provide?
  • A shared configuration (e.g., app settings, runtime version, hosting plan)
  • A common deployment context (you deploy the whole Function App, not individual functions)
  • Scaling and monitoring are applied at the Function App level
  • Functions within the same Function App share resources (e.g., memory, CPU)

Why Does Use Azure Function Insted of Azure App Service?

Use Azure Functions when:
  • You want simple, small, scalable, pay-per-use code.
  • You’re responding to events or triggers.
  • You don't want to manage servers.

Use Azure App Service when:
  • You need a full-featured web app or API with full control.
  • You're okay paying for always-on hosting.

Azure Functions vs. Azure Worker (Role/Service)

Feature / Aspect Azure Functions Azure Worker (Role/Service)
Architecture Serverless (event-driven) IaaS or PaaS (long-running background service)
Hosting Runs in Azure Functions platform Originally ran in Azure Cloud Services (now mostly replaced by Worker Service in App Service or containers)
Trigger-based? Yes – triggered by events (HTTP, Queue, Timer, etc.) No – runs continuously or as a background process
Execution Time Short-lived (default timeout ~5 mins; can be extended) Long-running by design
Scaling Auto-scales based on events (serverless) Manual or limited auto-scale (depends on hosting model)
Pricing Model Pay-per-execution (Consumption Plan) Pay-per-hosted instance (App Service Plan, VM, etc.)
Use Case Lightweight tasks, event-based jobs, automation Background processing, scheduled jobs, durable services
Statefulness Stateless by default Can maintain state during execution (e.g., loop, thread)
Deployment Function App (code + bindings) Hosted in App Service, Container, or VM
Languages C#, JavaScript, Python, Java, etc. C#, .NET Core (mostly)

Summary

Azure Functions simplifies cloud development by abstracting infrastructure, allowing you to focus solely on code. With C#, it becomes even more powerful for .NET developers to build scalable, event-driven applications quickly. I hope this was helpful to you.

Thanks

Kailash Chandra Behera

I am an IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال