How to Use Azure Function with C# - Simple Guide
To use an
Azure Function with C#, create a function app in Azure, write your function code in C# using the Azure Functions SDK, and deploy it. The function runs in response to triggers like HTTP requests or timers, allowing serverless execution without managing infrastructure.Syntax
An Azure Function in C# typically has a method decorated with a FunctionName attribute and a trigger attribute like HttpTrigger. The method parameters define the trigger input and output bindings.
- FunctionName: Names the function.
- HttpTrigger: Defines the function trigger type and authorization.
- HttpRequest req: Represents the incoming HTTP request.
- ILogger log: Used for logging inside the function.
csharp
using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; public static class MyFunction { [FunctionName("MyFunction")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); return new OkObjectResult("Hello from Azure Function!"); } }
Example
This example shows a simple HTTP-triggered Azure Function in C# that returns a greeting message. It logs the request and responds with a plain text message.
csharp
using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; public static class GreetingFunction { [FunctionName("GreetingFunction")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("GreetingFunction received a request."); string name = req.Query["name"]; if (string.IsNullOrEmpty(name)) { return new BadRequestObjectResult("Please pass a name on the query string."); } string responseMessage = $"Hello, {name}! Welcome to Azure Functions with C#."; return new OkObjectResult(responseMessage); } }
Output
HTTP 200 OK
Body: Hello, Alice! Welcome to Azure Functions with C#.
Common Pitfalls
- Forgetting to add the
FunctionNameattribute causes the function not to register. - Using incorrect trigger bindings or missing required parameters leads to runtime errors.
- Not setting the correct authorization level can block access or expose the function unintentionally.
- Ignoring async patterns can cause performance issues.
Always test your function locally before deploying.
csharp
/* Wrong: Missing FunctionName attribute */ public static class FaultyFunction { public static void Run() { // This function will not be recognized by Azure Functions runtime } } /* Right: Proper FunctionName attribute and signature */ public static class CorrectFunction { [FunctionName("CorrectFunction")] public static void Run() { // Function runs correctly } }
Quick Reference
Remember these key points when using Azure Functions with C#:
- Use
FunctionNameto name your function. - Choose the right trigger attribute like
HttpTrigger,TimerTrigger, etc. - Use
ILoggerfor logging. - Deploy via Azure CLI, Visual Studio, or GitHub Actions.
- Test locally with Azure Functions Core Tools.
Key Takeaways
Always decorate your C# function method with the FunctionName attribute to register it.
Use trigger attributes like HttpTrigger to define how your function is invoked.
Leverage ILogger for logging inside your function for easier debugging.
Test your Azure Function locally before deploying to avoid runtime errors.
Set appropriate authorization levels to secure your function endpoints.