Create your first function using Visual Studio

Azure Functions lets you execute your code in a serverless environment without having to first create a VM or publish a web application.

In this article, you learn how to use Visual Studio 2019 to locally create and test a "hello world" function and then publish it to Azure. This quickstart is designed for Visual Studio 2019. When creating a Functions project using Visual Studio 2017, you must first install the latest Azure Functions tools.

Function localhost response in the browser

Prerequisites

To complete this tutorial, you must first install Visual Studio 2019. Make sure that the Azure development workload is also installed.

Install Visual Studio with the Azure development workload

If you don't have an Azure subscription, create a free account before you begin.

Create a function app project

The Azure Functions project template in Visual Studio creates a project that can be published to a function app in Azure. You can use a function app to group functions as a logical unit for management, deployment, and sharing of resources.

  1. In Visual Studio, on the File menu, select New > Project.

  2. In the Create a new project dialog box, search for functions, choose the Azure Functions template, and select Next.

  3. Enter a name for your project, and select Create. The function app name must be valid as a C# namespace, so don't use underscores, hyphens, or any other nonalphanumeric characters.

  4. In Create a new Azure Functions application, use the following options:

    • Azure Functions v2 (.NET Core) 1
    • HTTP trigger
    • Storage Account: Storage Emulator
    • Authorization level: Anonymous
    Option Suggested value Description
    Functions runtime Azure Functions 2.x
    (.NET Core)
    This setting creates a function project that uses the version 2.x runtime of Azure Functions, which supports .NET Core. Azure Functions 1.x supports the .NET Framework. For more information, see Target Azure Functions runtime version.
    Function template HTTP trigger This setting creates a function triggered by an HTTP request.
    Storage Account Storage Emulator An HTTP trigger doesn't use the Azure Storage account connection. All other trigger types require a valid Storage account connection string. Because Functions requires a storage account, one is assigned or created when you publish your project to Azure.
    Authorization level Anonymous The created function can be triggered by any client without providing a key. This authorization setting makes it easy to test your new function. For more information about keys and authorization, see Authorization keys in the HTTP and webhook bindings.

    Note

    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.

  5. Select Create to create the function project and HTTP-triggered function.

Visual Studio creates a project and class that contains boilerplate code for the HTTP trigger function type. The FunctionName attribute on the method sets the name of the function, which by default is HttpTrigger. The HttpTrigger attribute specifies that the function is triggered by an HTTP request. The boilerplate code sends an HTTP response that includes a value from the request body or query string.

You can expand the capabilities of your function using input and output bindings by applying the appropriate attributes to the method. For more information, see the Triggers and bindings section of the Azure Functions C# developer reference.

Now that you've created your function project and an HTTP-triggered function, you can test it on your local computer.

Run the function locally

Visual Studio integrates with Azure Functions Core Tools so that you can test your functions locally using the full Functions runtime.

  1. To run your function, press F5. You may need to enable a firewall exception so that the tools can handle HTTP requests. Authorization levels are never enforced when running locally.

  2. Copy the URL of your function from the Azure Functions runtime output.

    Azure local runtime

  3. Paste the URL for the HTTP request into your browser's address bar. Append the query string ?name=<YOUR_NAME> to this URL and execute the request. The following shows the response in the browser to the local GET request returned by the function:

    Function localhost response in the browser

  4. To stop debugging, press Shift + F5.

After you have verified that the function runs correctly on your local computer, it's time to publish the project to Azure.

Publish the project to Azure

You must have a function app in your Azure subscription before you can publish your project. Visual Studio publishing creates a function app for you the first time you publish your project.

  1. In Solution Explorer, right-click the project and select Publish.

  2. In the Pick a publish target dialog, use the publish options as specified in the table below the image:

    Pick a publish target

    Option Description
    Azure Functions Consumption plan When you publish your project to a function app that runs in a Consumption plan, you only pay for executions of your functions app. Other hosting plans incur higher costs. To learn more, see Azure Functions scale and hosting.
    Create new A new function app, with related resources, is created in Azure. When you choose Select Existing, all files in the existing function app in Azure are overwritten by files from the local project. Only use this option when republishing updates to an existing function app.
    Run from package file Your function app is deployed using Zip Deploy with Run-From-Package mode enabled. This is the recommended way of running your functions, which results in better performance. When not using this option, make sure to stop your function app project from running locally before you publish to Azure.
  3. Select Publish. If you haven't already signed-in to your Azure account from Visual Studio, select Sign-in. You can also create a free Azure account.

  4. In the App Service: Create new dialog, use the Hosting settings as specified in the table below the image:

    Create App Service dialog

    Setting Suggested value Description
    Name Globally unique name Name that uniquely identifies your new function app. Valid characters are a-z, 0-9, and -.
    Subscription Choose your subscription The Azure subscription to use.
    Resource Group myResourceGroup Name of the resource group in which to create your function app. Choose New to create a new resource group.
    Hosting Plan Consumption plan Make sure to choose the Consumption under Size after you select New to create a serverless plan. Also, choose a Location in a region near you or near other services your functions access. When you run in a plan other than Consumption, you must manage the scaling of your function app.
    Azure Storage General-purpose storage account An Azure storage account is required by the Functions runtime. Select New to create a general-purpose storage account. You can also use an existing account that meets the storage account requirements.
  5. Select Create to create a function app and related resources in Azure with these settings and deploy your function project code.

  6. After the deployment is complete, make a note of the Site URL value, which is the address of your function app in Azure.

    Publish success message

Test your function in Azure

  1. Copy the base URL of the function app from the Publish profile page. Replace the localhost:port portion of the URL you used when testing the function locally with the new base URL. As before, make sure to append the query string ?name=<YOUR_NAME> to this URL and execute the request.

    The URL that calls your HTTP triggered function should be in the following format:

     http://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>?name=<YOUR_NAME> 
    
  2. Paste this new URL for the HTTP request into your browser's address bar. The following shows the response in the browser to the remote GET request returned by the function:

    Function response in the browser

Next steps

You have used Visual Studio to create and publish a C# function app in Azure with a simple HTTP triggered function. To learn more about developing functions as .NET class libraries, see Azure Functions C# developer reference.