Day 12 of 30-Day .NET Challenge: Azure Functions

Sukhpinder Singh - Mar 31 - - Dev Community

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

  1. Choose a project “Azure Function” from Visual Studio.

  2. Select .Net 6 as the target version

  3. Select the template “HTTP Trigger”

  4. Provide a function name.

  5. 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);
        }
    }
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode
Functions:

        BasicExample: [GET,POST] http://localhost:7073/api/BasicExample
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Add query parameters

Modify the URL with additional query parameters as ?name=Sukhpinder

    http://localhost:7073/api/BasicExample?name=Sukhpinder
Enter fullscreen mode Exit fullscreen mode

The response returned from the browser.

    Hello, Sukhpinder. This HTTP triggered function executed successfully.
Enter fullscreen mode Exit fullscreen mode

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .