Skip to content

Commit ebf0b73

Browse files
Merge pull request #108164 from anthonychu/20200318-durable-csharp-quickstart
Add VS Code C# Durable Functions quickstart
2 parents ed6ccb2 + 83228fe commit ebf0b73

9 files changed

+166
-13
lines changed

articles/azure-functions/durable/durable-functions-create-first-csharp.md

Lines changed: 158 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,166 @@
11
---
22
title: Create your first durable function in Azure using C#
3-
description: Create and publish an Azure Durable Function using Visual Studio.
3+
description: Create and publish an Azure Durable Function using Visual Studio or Visual Studio Code.
44
author: jeffhollan
55

66
ms.topic: quickstart
7-
ms.date: 11/02/2019
7+
ms.date: 03/18/2020
88
ms.author: azfuncdf
9+
zone_pivot_groups: code-editors-set-one
910

1011
---
12+
1113
# Create your first durable function in C\#
1214

1315
*Durable Functions* is an extension of [Azure Functions](../functions-overview.md) that lets you write stateful functions in a serverless environment. The extension manages state, checkpoints, and restarts for you.
1416

15-
[!INCLUDE [v1-note](../../../includes/functions-durable-v1-tutorial-note.md)]
17+
::: zone pivot="code-editor-vscode"
18+
19+
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 then publish the function code to Azure. These tools are available as part of the VS Code [Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions).
20+
21+
![Running durable function in Azure](./media/durable-functions-create-first-csharp/functions-vscode-complete.png)
22+
23+
## Prerequisites
24+
25+
To complete this tutorial:
26+
27+
* Install [Visual Studio Code](https://code.visualstudio.com/download).
28+
29+
* Install the following VS Code extensions:
30+
- [Azure Functions](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions)
31+
- [C#](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp)
32+
33+
* Make sure you have the latest version of the [Azure Functions Core Tools](../functions-run-local.md).
34+
35+
* Durable Functions require an Azure storage account. You need an Azure subscription.
36+
37+
* Make sure that you have version 3.1 or a later version of the [.NET Core SDK](https://dotnet.microsoft.com/download) installed.
38+
39+
[!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)]
40+
41+
## <a name="create-an-azure-functions-project"></a>Create your local project
42+
43+
In this section, you use Visual Studio Code to create a local Azure Functions project.
44+
45+
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...`.
46+
47+
![Create a function project](media/durable-functions-create-first-csharp/functions-vscode-create-project.png)
48+
49+
1. Choose an empty folder location for your project and choose **Select**.
50+
51+
1. Following the prompts, provide the following information:
52+
53+
| Prompt | Value | Description |
54+
| ------ | ----- | ----------- |
55+
| Select a language for your function app project | C# | Create a local C# Functions project. |
56+
| Select a version | Azure Functions v3 | 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. |
57+
| Select a template for your project's first function | Skip for now | |
58+
| Select how you would like to open your project | Open in current window | Reopens VS Code in the folder you selected. |
59+
60+
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](../functions-host-json.md) and [local.settings.json](../functions-run-local.md#local-settings-file) configuration files.
61+
62+
## Add functions to the app
63+
64+
The following steps use a template to create the durable function code in your project.
65+
66+
1. In the command palette, search for and select `Azure Functions: Create Function...`.
67+
68+
1. Following the prompts, provide the following information:
69+
70+
| Prompt | Value | Description |
71+
| ------ | ----- | ----------- |
72+
| Select a template for your function | DurableFunctionsOrchestration | Create a Durable Functions orchestration |
73+
| Provide a function name | HelloOrchestration | Name of the class in which functions are created |
74+
| Provide a namespace | Company.Function | Namespace for the generated class |
75+
76+
1. When VS Code prompts you to select a storage account, choose **Select storage account**. Following the prompts, provide the following information to create a new storage account in Azure.
77+
78+
| Prompt | Value | Description |
79+
| ------ | ----- | ----------- |
80+
| Select subscription | *name of your subscription* | Select your Azure subscription |
81+
| Select a storage account | Create a new storage account | |
82+
| Enter the name of the new storage account | *unique name* | Name of the storage account to create |
83+
| Select a resource group | *unique name* | Name of the resource group to create |
84+
| Select a location | *region* | Select a region close to you |
85+
86+
A class containing the new functions is added to the project. VS Code also adds the storage account connection string to *local.settings.json* and a reference to the [`Microsoft.Azure.WebJobs.Extensions.DurableTask`](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.DurableTask) NuGet package to the *.csproj* project file.
87+
88+
Open the new *HelloOrchestration.cs* file to view the contents. This durable function is a simple function chaining example with the following methods:
89+
90+
| Method | FunctionName | Description |
91+
| ----- | ------------ | ----------- |
92+
| **`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. |
93+
| **`SayHello`** | `HelloOrchestration_Hello` | The function returns a hello. It is the function that contains the business logic that is being orchestrated. |
94+
| **`HttpStart`** | `HelloOrchestration_HttpStart` | An [HTTP-triggered function](../functions-bindings-http-webhook.md) that starts an instance of the orchestration and returns a check status response. |
95+
96+
Now that you've created your function project and a durable function, you can test it on your local computer.
97+
98+
## Test the function locally
99+
100+
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.
101+
102+
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.
103+
104+
> [!NOTE]
105+
> Refer to the [Durable Functions Diagnostics](durable-functions-diagnostics.md#debugging) for more information on debugging.
106+
107+
1. In the **Terminal** panel, copy the URL endpoint of your HTTP-triggered function.
108+
109+
![Azure local output](media/durable-functions-create-first-csharp/functions-vscode-f5.png)
110+
111+
1. Using a tool like [Postman](https://www.getpostman.com/) or [cURL](https://curl.haxx.se/), send an HTTP POST request to the URL endpoint.
112+
113+
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.
114+
115+
1. Copy the URL value for `statusQueryGetUri` and paste it in the browser's address bar and execute the request. Alternatively you can also continue to use Postman to issue the GET request.
116+
117+
The request will query the orchestration instance for the status. You should get an eventual response, which shows us the instance has completed, and includes the outputs or results of the durable function. It looks like:
118+
119+
```json
120+
{
121+
"name": "HelloOrchestration",
122+
"instanceId": "9a528a9e926f4b46b7d3deaa134b7e8a",
123+
"runtimeStatus": "Completed",
124+
"input": null,
125+
"customStatus": null,
126+
"output": [
127+
"Hello Tokyo!",
128+
"Hello Seattle!",
129+
"Hello London!"
130+
],
131+
"createdTime": "2020-03-18T21:54:49Z",
132+
"lastUpdatedTime": "2020-03-18T21:54:54Z"
133+
}
134+
```
135+
136+
1. To stop debugging, press **Shift + F5** in VS Code.
137+
138+
After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.
139+
140+
[!INCLUDE [functions-create-function-app-vs-code](../../../includes/functions-sign-in-vs-code.md)]
141+
142+
[!INCLUDE [functions-publish-project-vscode](../../../includes/functions-publish-project-vscode.md)]
143+
144+
## Test your function in Azure
145+
146+
1. Copy the URL of the HTTP trigger from the **Output** panel. The URL that calls your HTTP-triggered function should be in the following format:
147+
148+
https://<functionappname>.azurewebsites.net/api/HelloOrchestration_HttpStart
16149

17-
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.
150+
1. 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.
151+
152+
## Next steps
153+
154+
You have used Visual Studio Code to create and publish a C# durable function app.
155+
156+
> [!div class="nextstepaction"]
157+
> [Learn about common durable function patterns](durable-functions-overview.md#application-patterns)
158+
159+
::: zone-end
160+
161+
::: zone pivot="code-editor-visualstudio"
162+
163+
In this article, you learn how to 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.
18164

19165
![Running durable function in Azure](./media/durable-functions-create-first-csharp/functions-vs-complete.png)
20166

@@ -34,7 +180,7 @@ The Azure Functions template creates a project that can be published to a functi
34180

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

37-
1. In the **Add a new project** dialog, search for `functions`, choose the **Azure Functions** template, and select **Next**.
183+
1. In the **Create a new project** dialog, search for `functions`, choose the **Azure Functions** template, and select **Next**.
38184

39185
![New project dialog to create a function in Visual Studio](./media/durable-functions-create-first-csharp/functions-vs-new-project.png)
40186

@@ -46,7 +192,7 @@ The Azure Functions template creates a project that can be published to a functi
46192

47193
| Setting | Suggested value | Description |
48194
| ------------ | ------- |----------------------------------------- |
49-
| **Version** | Azure Functions 2.0 <br />(.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](../functions-versions.md). |
195+
| **Version** | Azure Functions 3.0 <br />(.NET Core) | Creates a function project that uses the version 3.0 runtime of Azure Functions, which supports .NET Core 3.1. For more information, see [How to target Azure Functions runtime version](../functions-versions.md). |
50196
| **Template** | Empty | Creates an empty function app. |
51197
| **Storage account** | Storage Emulator | A storage account is required for durable function state management. |
52198

@@ -58,16 +204,13 @@ The following steps use a template to create the durable function code in your p
58204

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

61-
![Add new function](./media/durable-functions-create-first-csharp/functions-vs-add-new-function.png)
207+
![Add new function](./media/durable-functions-create-first-csharp/functions-vs-add-function.png)
62208

63209
1. Verify **Azure Function** is selected from the add menu, type a name for your C# file, and then select **Add**.
64210

65211
1. Select the **Durable Functions Orchestration** template and then select **Ok**
66212

67-
![Select durable template](./media/durable-functions-create-first-csharp/functions-vs-select-template.png)
68-
69-
> [!NOTE]
70-
> This template currently creates a durable function using an older 1.x version of the extension. See the [Durable Functions Versions](durable-functions-versions.md) article for information about how to upgrade to the newer 2.x versions of Durable Functions.
213+
![Select durable template](./media/durable-functions-create-first-csharp/functions-vs-select-template.png)
71214

72215
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:
73216

@@ -131,7 +274,7 @@ You must have a function app in your Azure subscription before you can publish y
131274

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

134-
http://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>_HttpStart
277+
https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>_HttpStart
135278

136279
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.
137280

@@ -140,4 +283,6 @@ You must have a function app in your Azure subscription before you can publish y
140283
You have used Visual Studio to create and publish a C# durable function app.
141284

142285
> [!div class="nextstepaction"]
143-
> [Learn about common durable function patterns](durable-functions-overview.md#application-patterns)
286+
> [Learn about common durable function patterns](durable-functions-overview.md#application-patterns)
287+
288+
::: zone-end
106 KB
Loading
272 KB
Loading
164 KB
Loading
203 KB
Loading
77.7 KB
Loading
140 KB
Loading

articles/zone-pivot-groups.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,11 @@ groups:
251251
title: C+
252252
- id: programming-language-java
253253
title: Java
254+
- id: code-editors-set-one
255+
title: Code Editors
256+
prompt: Choose a code editor
257+
pivots:
258+
- id: code-editor-vscode
259+
title: Visual Studio Code
260+
- id: code-editor-visualstudio
261+
title: Visual Studio

0 commit comments

Comments
 (0)