After we have created and published our application to azure , it’s time now to modernize it accelerate and simplify application development with serverless compute using the azure function .
The first azure function that we are going to create will play the role of add-task , so instead of using the API we will use this azure function to do the add task job .
Functions can be written in a variety of languages, and will automatically trigger and scale based on your application needs.
Creating the azure Function
In order to create our azure function ,first we need to clique on the solution in order to add a new project and than from the templates we are going to look for function .
next for the setting of the app we will follow the below table
PS : Make sure you set the Authorization level to Anonymous. If you choose the default level of Function, you’re required to present the function key in requests to access your function endpoint.
Visual Studio creates a project and class that contains boilerplate code for the HTTP trigger function type. The boilerplate code sends an HTTP response that includes a value from the request body or query string. The HttpTrigger
attribute specifies that the function is triggered by an HTTP request.
If you follow the video below on the blog we will end up having a function like this :
public class Function1 { private readonly TaskDbContext _TaskDbContext; public Function1(TaskDbContext TaskDbContext) { _TaskDbContext = TaskDbContext; } [FunctionName("CreateTodo")] public async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Creating a new todo list item"); var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var input = JsonConvert.DeserializeObject<ToDo>(requestBody); var todo = new ToDo { Name = input.Name, Priority = input.Priority, URD = System.DateTime.Now.ToString() }; await _TaskDbContext.todo.AddAsync(todo); await _TaskDbContext.SaveChangesAsync(); return new OkObjectResult(input); } }
The function will create a task based on the ToDo Model .
To see how to create and publish the function you can find detailed explanation below .