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 |
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.":::
To complete this tutorial:
-
Install Visual Studio Code.
-
Install the following Visual Studio Code extensions:
-
Make sure that you have the latest version of the Azure Functions Core Tools.
-
Durable Functions require an Azure storage account. You need an Azure subscription.
-
Make sure that you have version 3.1 or a later version of the .NET Core SDK installed.
[!INCLUDE quickstarts-free-trial-note]
In this section, you use Visual Studio Code to create a local Azure Functions project.
-
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.":::
-
Choose an empty folder location for your project and choose Select.
-
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.
The following steps use a template to create the durable function code in your project.
-
In the command palette, search for and select
Azure Functions: Create Function...
. -
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 -
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.
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.
-
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.
-
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.":::
-
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.
-
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" }
-
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]
-
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
-
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.
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.":::
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]
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.
-
In Visual Studio, select New > Project from the File menu.
-
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.":::
-
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.
-
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. -
Select Create to create an empty function project. This project has the basic configuration files needed to run your functions.
The following steps use a template to create the durable function code in your project.
-
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.":::
-
Verify Azure Function is selected from the add menu, enter a name for your C# file, and then select Add.
-
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.
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.
-
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.
:::image type="content" source="./media/durable-functions-create-first-csharp/functions-vs-debugging.png" alt-text="Screenshot of Azure local runtime.":::
-
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.
-
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" }
-
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.
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]
-
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
-
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.
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