| Azure-API-App | Azure-Function-Deployment-Steps | |
Azure Functions |
Azure Functions is a serverless compute service offered by Microsoft Azure that lets you run small pieces of code β called functions β without worrying about infrastructure. You write the logic, Azure handles the rest: provisioning, scaling, and managing servers. It automatically scales based on demand and you only pay for the execution time your code consumes.
A Timer Trigger runs based on a CRON expression. Here's how to define it in C#:
[Function("ScheduledTask")]
public void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
log.LogInformation($"Timer triggered at: {DateTime.Now}");
}
{second} {minute} {hour} {day} {month} {day-of-week}
"0 */5 * * * *" β Every 5 minutes"0 0 9 * * *" β Every day at 9 AM"0 0 * * 1" β Every Monday at midnightTimerInfo.ScheduleStatus to inspect next run timeAzureWebJobsStorage is configured in local.settings.jsonUseDevelopmentStorage=true for local testingAzure Functions support various triggers for event-driven execution. Timer Trigger is ideal for scheduled tasks like cleanup jobs, data sync, or reporting. Use CRON expressions to control when your function runs.
Hereβs a simple C# function that responds to an HTTP request:
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
public class HelloFunction
{
private readonly ILogger _logger;
public HelloFunction(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger();
}
[Function("HelloFunction")]
public HttpResponseData Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Hello from Azure Function!");
return response;
}
}
π‘ Use Case: This kind of function is perfect for lightweight APIs, webhooks, or automation endpoints.
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Get name from the query string
string name = req.RequestUri.ParseQueryString().Get("name");
// Return a response
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string")
: req.CreateResponse(HttpStatusCode.OK, "Hello, " + name);
}
When deployed, this Azure Function is triggered by an HTTP request. For example:
GET https://your-function-app.azurewebsites.net/api/hello?name=Alice
Hello, Alice
This example shows an HTTP-triggered Azure Function written in Node.js.
It takes a name query parameter and returns a greeting.
module.exports = async function (context, req) {
context.log("JavaScript HTTP trigger function processed a request.");
const name = req.query.name || (req.body && req.body.name);
if (name) {
context.res = {
status: 200,
body: "Hello, " + name
};
} else {
context.res = {
status: 400,
body: "Please pass a name in the query string or in the request body"
};
}
};
When deployed, this Azure Function is triggered by an HTTP request. Example call:
GET https://your-function-app.azurewebsites.net/api/hello?name=Alice
Hello, Alice
π‘ Pro Tip: Use JavaScript Azure Functions for lightweight APIs, automation scripts, or quick integrations with other services. Azure Functions are ideal for event-driven tasks like data processing, integrating with APIs, or running scheduled jobs.
| Azure-API-App | Azure-Function-Deployment-Steps | |