Build Azure Functions with Visual Studio 2019
    • Dark
      Light
    • PDF

    Build Azure Functions with Visual Studio 2019

    • Dark
      Light
    • PDF

    Article Summary

    #ServerlessTips - Azure Functions
    Author: Steef-Jan Wiggers Azure MVP

    Developers can build Azure Functions with various tools such as Visual Code, Browser (Azure Portal), and Visual Studio, With Integrated Developer Environments (IDE) Visual Code, and Visual Studio you will benefit from IntelliSense (code-completion), test, and debugging capabilities,

    With the latest Visual Studio 2019, you can author Azure Functions for .NET (version 1) or .NET Core (version 2 and 3) runtimes. Furthermore, you have templates for building Function according to supported bindings.
    Azure Functions in VS.png

    When creating a project, select the Azure Function Template, and subsequently the "Azure Functions vx" option from the Template drop-down. Next, you choose the template (trigger) of choice and target version.x by editing the project properties and select appropriate runtime if necessary.

    Depending on the trigger (binding), you will see some sample code of a possible implementation of your function. You can refactor to your own needs. Below a sample of an HTTP-triggered Azure Function:

    public static class Function1
        {
            [FunctionName("Function1")]
            public static async Task<IActionResult> Run(
                [HttpTrigger(AuthorizationLevel.Function, "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;
    
                return name != null
                    ? (ActionResult)new OkObjectResult($"Hello, {name}")
                    : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
            }
        }
    
    

    Once you have your code ready, build and test locally on your machine first before pushing it to Azure DevOps to deploy it to a Function App in Azure. Not when testing locally has either an Azure Storage Emulator installed on your machine or leverage a storage account in Azure. Furthermore, check the appropriate runtime for your function or download the runtime (usually when starting a function, it will ask if you want to download the runtime tools).

    Azure-functions.png


    Was this article helpful?