Skip to content

This Quickstart uses Azure Developer command-line (azd) tools to create functions that respond to HTTP requests. After testing the code locally, you deploy it to a new serverless function app you create running in a Flex Consumption plan in Azure Functions. This follows current best practices for secure and scalable Azure Functions deployments

License

Notifications You must be signed in to change notification settings

Azure-Samples/functions-quickstart-dotnet-azd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure Functions C# HTTP Trigger using Azure Developer CLI

This template repository contains an HTTP trigger reference sample for functions written in C# (isolated process mode) and deployed to Azure using the Azure Developer CLI (azd). The sample uses managed identity and a virtual network to make sure deployment is secure by default. You can opt out of a VNet being used in the sample by setting SKIP_VNET to true in the parameters.

This source code supports the article Quickstart: Create and deploy functions to Azure Functions using the Azure Developer CLI.

This project is designed to run on your local computer. You can also use GitHub Codespaces:

Open in GitHub Codespaces

This codespace is already configured with the required tools to complete this tutorial using either azd or Visual Studio Code. If you're working a codespace, skip down to Prepare your local environment.

Prerequisites

Initialize the local project

You can initialize a project from this azd template in one of these ways:

  • Use this azd init command from an empty local (root) folder:

    azd init --template functions-quickstart-dotnet-azd

    Supply an environment name, such as flexquickstart when prompted. In azd, the environment is used to maintain a unique deployment context for your app.

  • Clone the GitHub template repository locally using the git clone command:

    git clone https://github.com/Azure-Samples/functions-quickstart-dotnet-azd.git
    cd functions-quickstart-dotnet-azd

    You can also clone the repository from your own fork in GitHub.

Prepare your local environment

Navigate to the http app folder and create a file in that folder named local.settings.json that contains this JSON data:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
    }
}

Run your app from the terminal

  1. From the http folder, run this command to start the Functions host locally:

    func start
  2. From your HTTP test tool in a new terminal (or from your browser), call the HTTP GET endpoint: http://localhost:7071/api/httpget

  3. Test the HTTP POST trigger with a payload using your favorite secure HTTP test tool.

    Cmd\bash

    This example runs from the http folder and uses the curl tool with payload data from the testdata.json project file:

    curl -i http://localhost:7071/api/httppost -H "Content-Type: text/json" -d @testdata.json

    PowerShell

    You can also use this Invoke-RestMethod cmdlet in PowerShell from the http folder:

    Invoke-RestMethod -Uri http://localhost:7071/api/httppost -Method Post -ContentType "application/json" -InFile "testdata.json"
  4. When you're done, press Ctrl+C in the terminal window to stop the func.exe host process.

Run your app using Visual Studio Code

  1. Open the http app folder in a new terminal.
  2. Run the code . code command to open the project in Visual Studio Code.
  3. In the command palette (F1), type Azurite: Start, which enables debugging without warnings.
  4. Press Run/Debug (F5) to run in the debugger. Select Debug anyway if prompted about local emulator not running.
  5. Send GET and POST requests to the httpget and httppost endpoints respectively using your HTTP test tool (or browser for httpget). If you have the RestClient extension installed, you can execute requests directly from the test.http project file.

Run your app using Visual Studio

  1. Open the http.sln solution file in Visual Studio.
  2. Press Run/F5 to run in the debugger. Make a note of the localhost URL endpoints, including the port, which might not be 7071.
  3. Open the test.http project file, update the port on the localhost URL (if needed), and then use the built-in HTTP client to call the httpget and httppost endpoints.

Source Code

The function code for the httpget and httppost endpoints are defined in httpGetFunction.cs and httpPostBodyFunction.cs, respectively. The Function attribute applied to the async Run method sets the name of the function endpoint.

This code shows an HTTP GET (webhook):

[Function("httpget")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get")]
    HttpRequest req,
    string name)
{
    var returnValue = string.IsNullOrEmpty(name)
        ? "Hello, World."
        : $"Hello, {name}.";

    _logger.LogInformation($"C# HTTP trigger function processed a request for {returnValue}.");

    return new OkObjectResult(returnValue);
}

This code shows the HTTP POST that received a JSON formatted person object in the request body and returns a message using the values in the payload:

[Function("httppost")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
    [FromBody] Person person)
{
    _logger.LogInformation($"C# HTTP POST trigger function processed a request for url {req.Body}");

    if (string.IsNullOrEmpty(person.Name) | string.IsNullOrEmpty(person.Age.ToString()) | person.Age == 0)
    {
        _logger.LogInformation("C# HTTP POST trigger function processed a request with no name/age provided.");
        return new BadRequestObjectResult("Please provide both name and age in the request body.");
    }

    var returnValue = $"Hello, {person.Name}! You are {person.Age} years old.";
    
    _logger.LogInformation($"C# HTTP POST trigger function processed a request for {person.Name} who is {person.Age} years old.");
    return new OkObjectResult(returnValue);
}

Deploy to Azure

Run this command to provision the function app, with any required Azure resources, and deploy your code:

azd up

Alternatively, you can opt-out of a VNet being used in the sample. To do so, use azd env to configure SKIP_VNET to true before running azd up:

azd env set SKIP_VNET true
azd up

You're prompted to supply these required deployment parameters:

Parameter Description
Environment name An environment that's used to maintain a unique deployment context for your app. You won't be prompted if you created the local project using azd init.
Azure subscription Subscription in which your resources are created.
Azure location Azure region in which to create the resource group that contains the new Azure resources. Only regions that currently support the Flex Consumption plan are shown.

After publish completes successfully, azd provides you with the URL endpoints of your new functions, but without the function key values required to access the endpoints. To learn how to obtain these same endpoints along with the required function keys, see Invoke the function on Azure in the companion article Quickstart: Create and deploy functions to Azure Functions using the Azure Developer CLI.

Redeploy your code

You can run the azd up command as many times as you need to both provision your Azure resources and deploy code updates to your function app.

Note

Deployed code files are always overwritten by the latest deployment package.

Clean up resources

When you're done working with your function app and related resources, you can use this command to delete the function app and its related resources from Azure and avoid incurring any further costs:

azd down

About

This Quickstart uses Azure Developer command-line (azd) tools to create functions that respond to HTTP requests. After testing the code locally, you deploy it to a new serverless function app you create running in a Flex Consumption plan in Azure Functions. This follows current best practices for secure and scalable Azure Functions deployments

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published