Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
143 lines (87 sloc) 8.25 KB
title description author ms.topic
Create your first durable function in Azure using C#
Create and publish an Azure Durable Function using Visual Studio.

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.

[!INCLUDE v1-note]

In this article, you learn how to use the Visual Studio 2019 to locally create and test a "hello world" durable function. This function orchestrates and chains-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 2019.

Running durable function in Azure


To complete this tutorial:

  • Install Visual Studio 2019. Make sure that the Azure development workload is also installed. Visual Studio 2017 also supports Durable Functions development, but the UI and steps differ.

  • Verify you have the Azure Storage Emulator installed and running.

[!INCLUDE quickstarts-free-trial-note]

Create a function app project

The Azure Functions template 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 easier management, deployment, scaling, and sharing of resources.

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

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

    New project dialog to create a function in Visual Studio

  3. Type a Project name for your project, and select OK. The project 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 settings specified in the table that follows the image.

    Create a new Azure Functions Application dialog in Visual Studio

    Setting Suggested value Description
    Version Azure Functions 2.0
    (.NET Core)
    Creates a function project that uses the version 2.0 runtime of Azure Functions, which supports .NET Core. Azure Functions 1.0 supports the .NET Framework. For more information, see How to target Azure Functions runtime version.
    Template Empty Creates an empty function app.
    Storage account Storage Emulator A storage account is required for durable function state management.
  5. Select Create to create an empty function project. This project has the basic configuration files needed to run your functions.

Add functions to the app

The following steps use a template to create the durable function code in your 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, type a name for your C# file, and then select Add.

  3. Select the Durable Functions Orchestration template and then select Ok

    Select durable template

[!NOTE] This template currently creates a durable function using an older 1.x version of the extension. See the Durable Functions Versions article for information about how to upgrade to the newer 2.x versions of Durable Functions.

A new durable function is added to the app. Open the new .cs file to view the contents. This durable function is a simple function chaining example with the following methods:

Method FunctionName Description
RunOrchestrator <file-name> Manages the durable orchestration. In this case, the orchestration starts, creates a list, and adds the result of three functions calls to the list. When the three function calls are complete, it returns the list.
SayHello <file-name>_Hello The function returns a hello. It is the function that contains the business logic that is being orchestrated.
HttpStart <file-name>_HttpStart An HTTP-triggered function that starts an instance of the orchestration and returns 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 output 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": "2019-11-02T07:07:40Z",
        "lastUpdatedTime": "2019-11-02T07: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.

[!INCLUDE Publish the project to Azure]

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:

  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.

[!div class="nextstepaction"] Learn about common durable function patterns

You can’t perform that action at this time.