azure function is a way to implement serverless computing in azure.This means that the developer is not concerned about things such as managing infrastrcuture,scalling and deployment.Instead developer can focus on business logic.
Function is executed when an action occurs.Here action is the event which triggers the function.That is why you need to defined the trigger when defining ypur function.
There are different types of trigggers such as:
- HTTP Here function execute when there is HTTP Request
- Timer Function executes in a scehdule
- Queue storage Function executes based on the mesaages in a queue
- Event Hub Function executes when there is an event in event hub
You can create Azure function in Azure portal or Visual Studio or Visual Studio Code.Here we will look how to define function using Visual Studio Code
You declare Azure function in a static class as a static method:
public static class SimpleAzureFucntion
{
[FunctionName("SimpleAzureFucntion")]
public static async Task<IActionResult> Run()
{
}
}
As you can observe we have specified the Fucntion attribute here.This attribute is required when declaring functionThe function is named Run.
We now need to specify the trigger when this fucntion will be called.We specify this as an attribute on the fucntion argument.
Here we are soecifying the HttpTrigger on the first argument of type HttpRequest which will be contain the http request.
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, “get”, “post”, Route = null)] HttpRequest req,
We can also add other arguments to this fucntion like a normal function.
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, “get”, “post”, Route = null)] HttpRequest req, ILogger log)
{
Leave a Reply