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 the Visual Studio 2019 tools for Azure Functions to locally create and test a "hello world" function. You then publish the function code to Azure. These tools are available as part of the Azure development workload in Visual Studio 2019.
This topic includes a video that demonstrates the same basic steps.
To complete this tutorial:
Install Visual Studio 2019 and ensure that the Azure development workload is also installed.
Make sure you have the latest Azure Functions tools.
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.
In Visual Studio, on the File menu, select New > Project.
In the New Project dialog box, select Installed > Visual C# > Cloud > Azure Functions. Enter a name for your project, and select OK. The function app name must be valid as a C# namespace, so don't use underscores, hyphens, or any other nonalphanumeric characters.
Use the settings specified in the table that follows the image.
Setting Suggested value Description Version Azure Functions 2.x
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. 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. Access rights 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.
Make sure you set the Access rights 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.
Select OK to create the function project and HTTP-triggered function.
Visual Studio creates a project and in it a class that contains boilerplate code for the chosen function type. The FunctionName attribute on the method sets the name of the function. 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 add input and output bindings to a function 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.
Test the function locally
Azure Functions Core Tools lets you run an Azure Functions project on your local development computer. You are prompted to install these tools the first time you start a function from Visual Studio.
To test your function, press F5. If prompted, accept the request from Visual Studio to download and install Azure Functions Core (CLI) tools. You may also need to enable a firewall exception so that the tools can handle HTTP requests.
Copy the URL of your function from the Azure Functions runtime output.
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:
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. You can create a function app right from Visual Studio.
In Solution Explorer, right-click the project and select Publish.
Select Azure Function App, choose Create New, and then select Publish.
When you click Run from a package file (recommended), your function app will be deployed using Zip Deploy with Run-From-Package mode enabled. This is the recommended way of running your functions, and will result in better performance.
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.
If you haven't already connected Visual Studio to your Azure account, select Add an account....
In the Create App Service dialog, use the Hosting settings as specified in the table below the image:
Setting Suggested value Description App Name Globally unique name Name that uniquely identifies your new function app. 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. App Service Plan Consumption plan Make sure to choose the Consumption under Size after you click 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. Storage Account General-purpose storage account An Azure storage account is required by the Functions runtime. Click New to create a general-purpose storage account. You can also use an existing account that meets the storage account requirements.
Click Create to create a function app and related resources in Azure with these settings and deploy your function project code.
After the deployment is complete, make a note of the Site URL value, which is the address of your function app in Azure.
Test your function in Azure
Copy the base URL of the function app from the Publish profile page. Replace the
localhost:portportion 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:
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:
Watch the video
You have used Visual Studio to create and publish a C# function app with a simple HTTP triggered function.