Develop Azure Functions using Visual Studio

Azure Functions Tools for Visual Studio 2017 is an extension for Visual Studio that lets you develop, test, and deploy C# functions to Azure. If this experience is your first with Azure Functions, you can learn more at An introduction to Azure Functions.

The Azure Functions Tools provides the following benefits:

  • Edit, build, and run functions on your local development computer.
  • Publish your Azure Functions project directly to Azure.
  • Use WebJobs attributes to declare function bindings directly in the C# code instead of maintaining a separate function.json for binding definitions.
  • Develop and deploy pre-compiled C# functions. Pre-complied functions provide a better cold-start performance than C# script-based functions.
  • Code your functions in C# while having all of the benefits of Visual Studio development.

This article shows you how to use the Azure Functions Tools for Visual Studio 2017 to develop your functions in C#. You also learn how to publish your project to Azure as a .NET assembly.

Important

Don't mix local development with portal development in the same function app. When you publish from a local project to a function app, the deployment process overwrites any functions that you developed in the portal.

Prerequisites

Azure Functions Tools is included in the Azure development workload of Visual Studio 2017 version 15.5, or a later version. Make sure you include the Azure development workload in your Visual Studio 2017 installation:

Install Visual Studio 2017 with the Azure development workload

Make sure that your Visual Studio is up-to-date and that you are using the most recent version of the Azure Functions tools.

Other requirements

To create and deploy functions, you also need:

  • An active Azure subscription. If you don't have an Azure subscription, free accounts are available.

  • An Azure Storage account. To create a storage account, see Create a storage account.

Check your tools version

  1. From the Tools menu, choose Extensions and Updates. Expand Installed > Tools and choose Azure Functions and Web Jobs Tools.

    Verify the Functions tools version

  2. Note the installed Version. You can compare this version with the latest version listed in the release notes.

  3. If your version is older, update your tools in Visual Studio as shown in the following section.

Update your tools

  1. In the Extensions and Updates dialog, expand Updates > Visual Studio Marketplace, choose Azure Functions and Web Jobs Tools and select Update.

    Update the Functions tools version

  2. After the tools update is downloaded, close Visual Studio to trigger the tools update using the VSIX installer.

  3. In the installer, choose OK to start and then Modify to update the tools.

  4. After the update is complete, choose Close and restart Visual Studio.

Create an Azure Functions project

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

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

  2. In the New Project dialog, select Installed, expand Visual C# > Cloud, select Azure Functions, type a Name for your project, and click OK. The function app name must be valid as a C# namespace, so don't use underscores, hyphens, or any other nonalphanumeric characters.

    New project dialog to create a function in Visual Studio

  3. Use the settings specified in the table that follows the image.

    New function dialog in Visual Studio

    Setting Suggested value Description
    Version Azure Functions v1
    (.NET Framework)
    This creates a function project that uses the version 1 runtime of Azure Functions. The version 2 runtime, which supports .NET Core, is currently in preview. For more information, see How to target Azure Functions runtime version.
    Template HTTP trigger This creates a function triggered by an HTTP request.
    Storage account Storage Emulator An HTTP trigger doesn't use the 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.
  4. Click OK to create the function project and HTTP triggered function.

The project template creates a C# project, installs the Microsoft.NET.Sdk.Functions NuGet package, and sets the target framework. Functions 1.x targets the .NET Framework, and Functions 2.x targets .NET Standard. The new project has the following files:

  • host.json: Lets you configure the Functions host. These settings apply both when running locally and in Azure. For more information, see host.json reference.

  • local.settings.json: Maintains settings used when running functions locally. These settings are not used by Azure, they are used by the Azure Functions Core Tools. Use this file to specify app settings for variables required by your functions. Add a new item to the Values array for each connection required by the functions bindings in your project. For more information, see Local settings file in the Azure Functions Core Tools article.

For more information, see Functions class library project.

Configure the project for local development

The Functions runtime uses an Azure Storage account internally. For all trigger types other than HTTP and webhooks, you must set the Values.AzureWebJobsStorage key to a valid Azure Storage account connection string. Your function app can also use the Azure storage emulator for the AzureWebJobsStorage connection setting that is required by the project. To use the emulator, set the value of AzureWebJobsStorage to UseDevelopmentStorage=true. You must change this setting to an actual storage connection before deployment.

To set the storage account connection string:

  1. In Visual Studio, open Cloud Explorer, expand Storage Account > Your Storage Account, then select Properties and copy the Primary Connection String value.

  2. In your project, open the local.settings.json file and set the value of the AzureWebJobsStorage key to the connection string you copied.

  3. Repeat the previous step to add unique keys to the Values array for any other connections required by your functions.

Create a function

In pre-compiled functions, the bindings used by the function are defined by applying attributes in the code. When you use the Azure Functions Tools to create your functions from the provided templates, these attributes are applied for you.

  1. In Solution Explorer, right-click on your project node and select Add > New Item. Select Azure Function, type a Name for the class, and click Add.

  2. Choose your trigger, set the binding properties, and click Create. The following example shows the settings when creating a Queue storage triggered function.

    Create a queue triggered function

    This trigger example uses a connection string with a key named QueueStorage. This connection string setting must be defined in the local.settings.json file.

  3. Examine the newly added class. You see a static Run method, that is attributed with the FunctionName attribute. This attribute indicates that the method is the entry point for the function.

    For example, the following C# class represents a basic Queue storage triggered function:

    using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    
    namespace FunctionApp1
    {
        public static class Function1
        {
            [FunctionName("QueueTriggerCSharp")]
            public static void Run([QueueTrigger("myqueue-items", Connection = "QueueStorage")]string myQueueItem, TraceWriter log)
            {
                log.Info($"C# Queue trigger function processed: {myQueueItem}");
            }
        }
    }
    

    A binding-specific attribute is applied to each binding parameter supplied to the entry point method. The attribute takes the binding information as parameters. In the previous example, the first parameter has a QueueTrigger attribute applied, indicating queue triggered function. The queue name and connection string setting name are passed as parameters to the QueueTrigger attribute. For more information, see Azure Queue storage bindings for Azure Functions.

You can use the above procedure to add more functions to your function app project. Each function in the project can have a different trigger, but a function must have exactly one trigger. For more information, see Azure Functions triggers and bindings concepts.

Add bindings

As with triggers, input and output bindings are added to your function as binding attributes. Add bindings to a function as follows:

  1. Make sure you have configured the project for local development.

  2. Add the appropriate NuGet extension package for the specific binding. For more information, see Local C# development using Visual Studio in the Triggers and Bindings article. The binding-specific NuGet package requirements are found in the reference article for the binding. For example, find package requirements for the Event Hubs trigger in the Event Hubs binding reference article.

  3. If there are app settings that the binding needs, add them to the Values collection in the local setting file. These values are used when the function runs locally. When the function runs in the function app in Azure, the function app settings are used.

  4. Add the appropriate binding attribute to the method signature. In the following example, a queue message triggers the function, and the output binding creates a new queue message with the same text in a different queue.

    public static class SimpleExampleWithOutput
    {
        [FunctionName("CopyQueueMessage")]
        public static void Run(
            [QueueTrigger("myqueue-items-source", Connection = "AzureWebJobsStorage")] string myQueueItem, 
            [Queue("myqueue-items-destination", Connection = "AzureWebJobsStorage")] out string myQueueItemCopy,
            TraceWriter log)
        {
            log.Info($"CopyQueueMessage function processed: {myQueueItem}");
            myQueueItemCopy = myQueueItem;
        }
    }
    

    The connection to Queue storage is obtained from the AzureWebJobsStorage setting. For more information, see the reference article for the specific binding.

The following table shows the bindings that are supported in the two major versions of the Azure Functions runtime.

Type 1.x 2.x Trigger Input Output
Blob Storage 1
Cosmos DB
Event Grid
Event Hubs
External File2
External Table2
HTTP 1
Microsoft Graph
Excel tables
Microsoft Graph
OneDrive files
Microsoft Graph
Outlook email
Microsoft Graph
Events
Microsoft Graph
Auth tokens
Mobile Apps
Notification Hubs
Queue storage 1
SendGrid
Service Bus
Table storage 1
Timer
Twilio
Webhooks

1 In 2.x, all bindings except HTTP, Timer, and Azure Storage must be registered. See Register binding extensions.

2 Experimental — not supported and might be abandoned in the future.

Testing functions

Azure Functions Core Tools lets you run 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.

With the project running, you can test your code as you would test deployed function. For more information, see Strategies for testing your code in Azure Functions. When running in debug mode, breakpoints are hit in Visual Studio as expected.

For an example of how to test a queue triggered function, see the queue triggered function quickstart tutorial.

To learn more about using the Azure Functions Core Tools, see Code and test Azure functions locally.

Publish to Azure

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

  2. Select Azure Function App, choose Create New, and then select Publish.

    Pick a publish target

  3. If you haven't already connected Visual Studio to your Azure account, select Add an account....

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

    Create App Service dialog

    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.
  5. Click 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

Function app settings

Any settings you added in the local.settings.json must be also added to the function app in Azure. These settings are not uploaded automatically when you publish the project.

The easiest way to upload the required settings to your function app in Azure is to use the Manage Application Settings... link that is displayed after you successfully publish your project.

This displays the Application Settings dialog for the function app, where you can add new application settings or modify existing ones.

You can also manage application settings in one of these other ways:

Next steps

For more information about Azure Functions Tools, see the Common Questions section of the Visual Studio 2017 Tools for Azure Functions blog post.

To learn more about the Azure Functions Core Tools, see Code and test Azure functions locally.

To learn more about developing functions as .NET class libraries, see Azure Functions C# developer reference. This article also links to examples of how to use attributes to declare the various types of bindings supported by Azure Functions.