Introduction
Azure Functions are serverless applications on Microsoft Azure Cloud Platform without worrying about the infrastructure to run it. It’s very similar to the lambda function in the AWS Cloud.
Learning Objectives
Create a function using Visual Studio or Visual Studio Code
Run an Azure function locally.
Prerequisites for Developers
Experience with Visual Studio
Basic understanding of C# Programming Language
Getting Started
Add a new function
Choose a project “Azure Function” from Visual Studio.
Select .Net 6 as the target version
Select the template “HTTP Trigger”
Provide a function name.
Choose Authorization Level as “Anonymous” which allows access to anyone to call your function endpoint.
Code
The below Azure Function returns a string message as follows
If ?name= is passed then returns a message as Hello, {name}. This HTTP triggered function executed successfully.
Else a general message is returned This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.
public static class BasicExample
{
[FunctionName("BasicExample")]
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"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
Test the function locally
Simply press F5 to start debugging the Azure function, it will open a console which will provide a URL to access the browser.
Console Output
Azure Functions Core Tools
Core Tools Version: 4.0.5198 Commit hash: N/A (64-bit)
Function Runtime Version: 4.21.1.20667
[2024-03-28T05:48:45.707Z] Found D:\Workspace\30DayChallenge.Net\AzureFunctionExample\AzureFunctionExample.csproj. Using for user secrets file configuration.
Functions:
BasicExample: [GET,POST] http://localhost:7073/api/BasicExample
Open the URL
Open the URL “http://localhost:7073/api/BasicExample” in the browser to start running the function endpoint.
The response returned from the browser.
This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.
Add query parameters
Modify the URL with additional query parameters as ?name=Sukhpinder
http://localhost:7073/api/BasicExample?name=Sukhpinder
The response returned from the browser.
Hello, Sukhpinder. This HTTP triggered function executed successfully.
Complete Code on GitHub
GitHub — ssukhpinder/30DayChallenge.Net
C# Programming🚀
Thank you for being a part of the C# community! Before you leave:
If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️
Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr
Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev
More content at C# Programming