Create your first durable function in C#

Durable Functions is an extension of Azure Functions that lets you write stateful functions in a serverless environment. The extension manages state, checkpoints, and restarts for you.

In this article, you learn how to use the Visual Studio 2017 tools for Azure Functions to locally create and test a "hello world" durable function. This function will orchestrate and chain together calls to other functions. You then publish the function code to Azure. These tools are available as part of the Azure development workload in Visual Studio 2017.

Running durable function in Azure

Prerequisites

To complete this tutorial:

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. 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 2.x
    (.NET Core)
    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 How to target Azure Functions runtime version.
    Template Empty This creates an empty function app.
    Storage account Storage Emulator A storage account is required for durable function state management.
  4. Click OK to create an empty function project.

Add functions to the app

Visual Studio creates an empty function app project. It contains the basic configuration files needed for an app, but doesn't yet contain any functions. We'll need to add a durable function template to the project.

  1. Right-click the project in Visual Studio and select Add > New Azure Function.

    Add new function

  2. Verify Azure Function is selected from the add menu, and give your C# file a name. Press Add.

  3. Select the Durable Functions Orchestration template and click Ok

    Select durable template

A new durable function will be added to the app. Open the new file to view the contents. This durable function is a simple function chaining example.

  • The RunOrchestrator method is associated with the orchestrator function. This function will start, create a list, and add the result of three functions calls to the list. When the three function calls are completed, it will return the list. The function it is calling is the SayHello method (default it will be called "_Hello").
  • The SayHello function will return a hello.
  • The HttpStart method describes the function that will start instances of the orchestration. It is associated with an HTTP trigger that will start a new instance of the orchestrator and return back a check status response.

Now that you've created your function project and a durable 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.

  1. 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.

  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 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

    The response is the initial result from the HTTP function letting us know the durable orchestration has started successfully. It is not yet the end result of the orchestration. The response includes a few useful URLs. For now, let's query the status of the orchestration.

  4. Copy the URL value for statusQueryGetUri and pasting it in the browser's address bar and execute the request.

    The request will query the orchestration instance for the status. You should get an eventual response that looks like the following. This shows us the instance has completed, and includes the outputs or results of the durable function.

    {
        "instanceId": "d495cb0ac10d4e13b22729c37e335190",
        "runtimeStatus": "Completed",
        "input": null,
        "customStatus": null,
        "output": [
            "Hello Tokyo!",
            "Hello Seattle!",
            "Hello London!"
        ],
        "createdTime": "2018-11-08T07:07:40Z",
        "lastUpdatedTime": "2018-11-08T07:07:52Z"
    }
    
  5. 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.

  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

    When you enable Run from Zip, your function app in Azure is run directly from the deployment package. For more information, see Run your Azure Functions from a package file.

    Caution

    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.

  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

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.

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

     http://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>_HttpStart
    
  2. Paste this new URL for the HTTP request into your browser's address bar. You should get the same status response as before when using the published app.

Next steps

You have used Visual Studio to create and publish a C# durable function app.