|
| 1 | +# Hello World with the Durable Task SDK for .NET |
| 2 | + |
| 3 | +In addition to [Durable Functions](https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-overview), the [Durable Task SDK for .NET](https://github.com/microsoft/durabletask-dotnet) can also use the Durable Task Scheduler service for managing orchestration state. |
| 4 | + |
| 5 | +This directory includes a sample .NET console app that demonstrates how to use the Durable Task Scheduler with the Durable Task SDK for .NET (without any Azure Functions dependency). |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) |
| 10 | +- [PowerShell](https://docs.microsoft.com/powershell/scripting/install/installing-powershell) |
| 11 | +- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) |
| 12 | + |
| 13 | +## Creating a Durable Task Scheduler task hub |
| 14 | + |
| 15 | +Before you can run the app, you need to create a Durable Task Scheduler task hub in Azure and produce a connection string that references it. |
| 16 | + |
| 17 | +> **NOTE**: These are abbreviated instructions for simplicity. For a full set of instructions, see the Azure Durable Functions [QuickStart guide](../../../../quickstarts/HelloCities/README.md#create-a-durable-task-scheduler-namespace-and-task-hub). |
| 18 | +
|
| 19 | +1. Install the Durable Task Scheduler CLI extension: |
| 20 | + |
| 21 | + ```bash |
| 22 | + az upgrade |
| 23 | + az extension add --name durabletask --allow-preview true |
| 24 | + ``` |
| 25 | + |
| 26 | +1. Create a resource group: |
| 27 | + |
| 28 | + ```powershell |
| 29 | + az group create --name my-resource-group --location northcentralus |
| 30 | + ``` |
| 31 | + |
| 32 | +1. Create a Durable Task Scheduler namespace: |
| 33 | + |
| 34 | + ```powershell |
| 35 | + az durabletask namespace create -g my-resource-group --name my-namespace |
| 36 | + ``` |
| 37 | + |
| 38 | +1. Create a task hub within the namespace: |
| 39 | + |
| 40 | + ```powershell |
| 41 | + az durabletask taskhub create -g my-resource-group --namespace my-namespace --name "portable-dotnet" |
| 42 | + ``` |
| 43 | + |
| 44 | +1. Grant the current user permission to connect to the `portable-dotnet` task hub: |
| 45 | + |
| 46 | + ```powershell |
| 47 | + $subscriptionId = az account show --query "id" -o tsv |
| 48 | + $loggedInUser = az account show --query "user.name" -o tsv |
| 49 | +
|
| 50 | + az role assignment create ` |
| 51 | + --assignee $loggedInUser ` |
| 52 | + --role "Durable Task Data Contributor" ` |
| 53 | + --scope "/subscriptions/$subscriptionId/resourceGroups/my-resource-group/providers/Microsoft.DurableTask/namespaces/my-namespace/taskHubs/portable-dotnet" |
| 54 | + ``` |
| 55 | + |
| 56 | + Note that it may take a minute for the role assignment to take effect. |
| 57 | + |
| 58 | +1. Get the endpoint for the scheduler resource and save it to the `DURABLE_TASK_SCHEDULER_ENDPOINT_ADDRESS` environment variable: |
| 59 | + |
| 60 | + ```powershell |
| 61 | + $endpoint = az durabletask namespace show ` |
| 62 | + -g my-resource-group ` |
| 63 | + -n my-namespace ` |
| 64 | + --query "properties.url" ` |
| 65 | + -o tsv |
| 66 | + $env:DURABLE_TASK_SCHEDULER_ENDPOINT_ADDRESS = $endpoint |
| 67 | + ``` |
| 68 | + |
| 69 | + The `DURABLE_TASK_SCHEDULER_ENDPOINT_ADDRESS` environment variable is used by the sample app to connect to the Durable Task Scheduler resource. |
| 70 | + |
| 71 | +1. Save the task hub name to the `DURABLE_TASK_SCHEDULER_TASK_HUB_NAME` environment variable: |
| 72 | + |
| 73 | + ```powershell |
| 74 | + $env:DURABLE_TASK_SCHEDULER_TASK_HUB_NAME = "portable-dotnet" |
| 75 | + ``` |
| 76 | + |
| 77 | + The `DURABLE_TASK_SCHEDULER_TASK_HUB_NAME` environment variable is to configure the sample app with the correct task hub resource name. |
| 78 | + |
| 79 | +## Running the sample |
| 80 | + |
| 81 | +In the same terminal window as above, use the following steps to run the sample on your local machine. |
| 82 | + |
| 83 | +1. Clone this repository. |
| 84 | + |
| 85 | +1. Open a terminal window and navigate to the `samples/portable-sdk/dotnet/AspNetWebApp` directory. |
| 86 | + |
| 87 | +1. Run the following command to build and run the sample: |
| 88 | + |
| 89 | + ```bash |
| 90 | + dotnet run |
| 91 | + ``` |
| 92 | + |
| 93 | +You should see output similar to the following: |
| 94 | + |
| 95 | +```plaintext |
| 96 | +Building... |
| 97 | +info: Microsoft.DurableTask[1] |
| 98 | + Durable Task gRPC worker starting. |
| 99 | +info: Microsoft.Hosting.Lifetime[14] |
| 100 | + Now listening on: http://localhost:5008 |
| 101 | +info: Microsoft.Hosting.Lifetime[0] |
| 102 | + Application started. Press Ctrl+C to shut down. |
| 103 | +info: Microsoft.Hosting.Lifetime[0] |
| 104 | + Hosting environment: Development |
| 105 | +info: Microsoft.Hosting.Lifetime[0] |
| 106 | + Content root path: D:\projects\Azure-Functions-Durable-Task-Scheduler-Private-Preview\samples\portable-sdk\dotnet\AspNetWebApp |
| 107 | +info: Microsoft.DurableTask[4] |
| 108 | + Sidecar work-item streaming connection established. |
| 109 | +``` |
| 110 | + |
| 111 | +## View orchestrations in the dashboard |
| 112 | + |
| 113 | +You can view the orchestrations in the Durable Task Scheduler dashboard by navigating to the namespace-specific dashboard URL in your browser. |
| 114 | + |
| 115 | +Use the following PowerShell command to get the dashboard URL: |
| 116 | + |
| 117 | +```powershell |
| 118 | +$baseUrl = az durabletask namespace show ` |
| 119 | + -g my-resource-group ` |
| 120 | + -n my-namespace ` |
| 121 | + --query "properties.dashboardUrl" ` |
| 122 | + -o tsv |
| 123 | +$dashboardUrl = "$baseUrl/taskHubs/portable-dotnet" |
| 124 | +$dashboardUrl |
| 125 | +``` |
| 126 | + |
| 127 | +The URL should look something like the following: |
| 128 | + |
| 129 | +```plaintext |
| 130 | +https://my-namespace-atdngmgxfsh0-db.northcentralus.durabletask.io/taskHubs/portable-dotnet |
| 131 | +``` |
| 132 | + |
| 133 | +Once logged in, you should see the orchestrations that were created by the sample app. Below is an example of what the dashboard might look like (note that some of the details will be different than the screenshot): |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +## Optional: Deploy to Azure Container Apps |
| 139 | +1. Create an container app following the instructions in the [Azure Container App documentation](https://learn.microsoft.com/azure/container-apps/get-started?tabs=bash). |
| 140 | +2. During step 1, specify the deployed container app code folder at samples\portable-sdk\dotnet\AspNetWebApp |
| 141 | +3. Follow the instructions to create a user managed identity and assign the `Durable Task Data Contributor` role then attach it to the container app you created in step 1 at [Azure-Functions-Durable-Task-Scheduler-Private-Preview](..\..\..\..\docs\configure-existing-app.md#run-the-app-on-azure-net). Please skip section "Add required environment variables to app" since these environment variables are not required for deploying to container app. |
| 142 | +4. Call the container app endpoint at `http://sampleapi-<your-container-app-name>.azurecontainerapps.io/api/orchestrators/HelloCities`, Sample curl command: |
| 143 | + |
| 144 | + ```bash |
| 145 | + curl -X POST "https://sampleapi-<your-container-app-name>.azurecontainerapps.io/api/orchestrators/HelloCities" |
| 146 | + ``` |
| 147 | +5. You should see the orchestration created in the Durable Task Scheduler dashboard. |
0 commit comments