Skip to content

Latest commit

 

History

History
289 lines (181 loc) · 17.1 KB

durable-functions-create-first-csharp.md

File metadata and controls

289 lines (181 loc) · 17.1 KB
title description author ms.topic ms.date ms.author zone_pivot_groups ms.devlang ms.custom
Create your first durable function in Azure using C#
Create and publish an Azure Durable Function using Visual Studio or Visual Studio Code.
anthonychu
quickstart
06/15/2022
azfuncdf
code-editors-set-one
csharp
mode-other, devdivchpfy22, vscode-azure-extension-update-complete

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.

::: zone pivot="code-editor-vscode"

In this article, you learn how to use Visual Studio Code to locally create and test a "hello world" durable function. This function orchestrates and chains together calls to other functions. You can then publish the function code to Azure. These tools are available as part of the Visual Studio Code Azure Functions extension.

:::image type="content" source="./media/durable-functions-create-first-csharp/functions-vscode-complete.png" alt-text="Screenshot of Visual Studio Code window with a durable function.":::

Prerequisites

To complete this tutorial:

[!INCLUDE quickstarts-free-trial-note]

Create your local project

In this section, you use Visual Studio Code to create a local Azure Functions project.

  1. In Visual Studio Code, press F1 (or Ctrl/Cmd+Shift+P) to open the command palette. In the command palette, search for and select Azure Functions: Create New Project....

    :::image type="content" source="media/durable-functions-create-first-csharp/functions-vscode-create-project.png" alt-text="Screenshot of create a function project window.":::

  2. Choose an empty folder location for your project and choose Select.

  3. Follow the prompts and provide the following information:

    Prompt Value Description
    Select a language for your function app project C# Create a local C# Functions project.
    Select a version Azure Functions v4 You only see this option when the Core Tools aren't already installed. In this case, Core Tools are installed the first time you run the app.
    Select a template for your project's first function Skip for now
    Select how you would like to open your project Open in current window Reopens Visual Studio Code in the folder you selected.

Visual Studio Code installs the Azure Functions Core Tools if needed. It also creates a function app project in a folder. This project contains the host.json and local.settings.json configuration files.

Add functions to the app

The following steps use a template to create the durable function code in your project.

  1. In the command palette, search for and select Azure Functions: Create Function....

  2. Follow the prompts and provide the following information:

    Prompt Value Description
    Select a template for your function DurableFunctionsOrchestration Create a Durable Functions orchestration
    Provide a function name HelloOrchestration Name of the class in which functions are created
    Provide a namespace Company.Function Namespace for the generated class
  3. When Visual Studio Code prompts you to select a storage account, choose Select storage account. Follow the prompts and provide the following information to create a new storage account in Azure:

    Prompt Value Description
    Select subscription name of your subscription Select your Azure subscription
    Select a storage account Create a new storage account
    Enter the name of the new storage account unique name Name of the storage account to create
    Select a resource group unique name Name of the resource group to create
    Select a location region Select a region close to you

A class containing the new functions is added to the project. Visual Studio Code also adds the storage account connection string to local.settings.json and a reference to the Microsoft.Azure.WebJobs.Extensions.DurableTask NuGet package to the .csproj project file.

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

Method FunctionName Description
RunOrchestrator HelloOrchestration 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 HelloOrchestration_Hello The function returns a hello. It's the function that contains the business logic that is being orchestrated.
HttpStart HelloOrchestration_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're prompted to install these tools the first time you start a function from Visual Studio Code.

  1. To test your function, set a breakpoint in the SayHello activity function code and press F5 to start the function app project. Output from Core Tools is displayed in the Terminal panel.

    [!NOTE] For more information on debugging, see Durable Functions Diagnostics.

  2. In the Terminal panel, copy the URL endpoint of your HTTP-triggered function.

    :::image type="content" source="media/durable-functions-create-first-csharp/functions-vscode-f5.png" alt-text="Screenshot of Azure local output window.":::

  3. Use a tool like Postman or cURL, and then send an HTTP POST request to the URL endpoint.

    The response is the HTTP function's initial result, letting us know that the durable orchestration has started successfully. It isn't 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, paste it into the browser's address bar, and execute the request. Alternatively, you can also continue to use Postman to issue the GET request.

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

    {
        "name": "HelloOrchestration",
        "instanceId": "9a528a9e926f4b46b7d3deaa134b7e8a",
        "runtimeStatus": "Completed",
        "input": null,
        "customStatus": null,
        "output": [
            "Hello Tokyo!",
            "Hello Seattle!",
            "Hello London!"
        ],
        "createdTime": "2020-03-18T21:54:49Z",
        "lastUpdatedTime": "2020-03-18T21:54:54Z"
    }
  5. To stop debugging, press Shift + F5 in Visual Studio Code.

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

[!INCLUDE functions-sign-in-vs-code]

[!INCLUDE functions-publish-project-vscode]

Test your function in Azure

  1. Copy the URL of the HTTP trigger from the Output panel. The URL that calls your HTTP-triggered function must be in the following format:

    https://<functionappname>.azurewebsites.net/api/HelloOrchestration_HttpStart

  2. Paste this new URL for the HTTP request into your browser's address bar. You must get the same status response as before when using the published app.

Next steps

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

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

::: zone-end

::: zone pivot="code-editor-visualstudio"

In this article, you learn how to use Visual Studio 2022 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 2022.

:::image type="content" source="./media/durable-functions-create-first-csharp/functions-vs-complete.png" alt-text="Screenshot of Visual Studio 2019 window with a durable function.":::

Prerequisites

To complete this tutorial:

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

  • Verify that you have the Azurite 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 Create a new project dialog, search for functions, choose the Azure Functions template, and then select Next.

    :::image type="content" source="./media/durable-functions-create-first-csharp/functions-vs-new-project.png" alt-text="Screenshot of new project dialog to create a function in Visual Studio.":::

  3. Enter 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 nonalphanumeric characters.

  4. Under Additional information, use the settings specified in the table that follows the image.

    :::image type="content" source="./media/durable-functions-create-first-csharp/functions-vs-new-function.png" alt-text="Screenshot of create a new Azure Functions Application dialog in Visual Studio.":::

    Setting Suggested value Description
    Functions worker .NET 6 Creates a function project that supports .NET 6 and the Azure Functions Runtime 4.0. For more information, see How to target Azure Functions runtime version.
    Function 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.

    :::image type="content" source="./media/durable-functions-create-first-csharp/functions-vs-add-function.png" alt-text="Screenshot of Add new function.":::

  2. Verify Azure Function is selected from the add menu, enter a name for your C# file, and then select Add.

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

    :::image type="content" source="./media/durable-functions-create-first-csharp/functions-vs-select-durable-template.png" alt-text="Screenshot of Select durable template.":::

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's 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.

You can test it on your local computer now that you've created your function project and a durable function.

Test the function locally

Azure Functions Core Tools lets you run an Azure Functions project on your local development computer. You're 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.

    :::image type="content" source="./media/durable-functions-create-first-csharp/functions-vs-debugging.png" alt-text="Screenshot of 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:

    :::image type="content" source="./media/durable-functions-create-first-csharp/functions-vs-status.png" alt-text="Screenshot of the browser window with statusQueryGetUri called out.":::

    The response is the HTTP function's initial result, letting us know that the durable orchestration has started successfully. It isn't 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, paste it into the browser's address bar, and execute the request.

    The request will query the orchestration instance for the status. You must 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.

    {
        "name": "Durable",
        "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've 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 publishing 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 must be in the following format:

    https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>_HttpStart

  2. Paste this new URL for the HTTP request into your browser's address bar. You must 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

::: zone-end