Azure Functions has an extension called Durable Functions. Durable Functions helps you easily orchestrate stateful logic, making it an excellent solution for workflow scenarios, as well as stateful patterns like fan-out/fan-in and workloads that require long-running operations or need to wait arbitrarily long for external events.
This sample shows how to implement an order processing workflow with Durable Functions in C# (running in the isolated model) and can easily be deployed to a function app in Azure. Durable Functions needs a "storage backend provider" to persist application states. This sample uses the default backend, which is Azure Storage.
Important
This sample creates several resources. Make sure to delete the resource group after testing to minimize charges!
The project is designed to run on your local computer, provided you have met the required prerequisites. You can run the project locally in these environments:
-
Start Azurite storage emulator. See this page for how to configure and start the Azurite emulator for Local Storage.
-
Clone the repo, then create a file named
local.settings.json
with the following content in the OrderProcessor directory:{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated" } }
- Open a new terminal and do the following:
cd OrderProcessor
func start
- This sample uses an HTTP trigger to start an orchestration, so open a browser and go to http://localhost:7071/api/OrderProcessingOrchestration_HttpStart. You should see something similar to the following:
{
"id": "e838bdb52db24560a6b30c261ac2985d",
"purgeHistoryDeleteUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/e838bdb52db24560a6b30c261ac2985d?code=<code>",
"sendEventPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/e838bdb52db24560a6b30c261ac2985d/raiseEvent/{eventName}?code=<code>",
"statusQueryGetUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/e838bdb52db24560a6b30c261ac2985d?code=<code>",
"terminatePostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/e838bdb52db24560a6b30c261ac2985d/terminate?reason={{text}}}&code=<code>"
}
- To check the status of the orchestration instance started, go to the
statusQueryGetUri
. Your orchestration instance should show status "Running". After a few seconds, refresh to see that the orchestration instance is "Completed" and what the output is.
{
"name": "OrderProcessingOrchestration",
"instanceId": "e838bdb52db24560a6b30c261ac2985d",
"runtimeStatus": "Completed",
"input": {
"Name": "milk",
"TotalCost": 5,
"Quantity": 1
},
"customStatus": null,
"output": {
"Processed": true
},
"createdTime": "2024-09-12T00:29:07Z",
"lastUpdatedTime": "2024-09-12T00:29:31Z"
}
- Open project using Visual Studio 2022 or later.
- Press Run/F5 to run in the debugger
- Use same approach above to start an orchestration instance and check its status.
- Open this folder in a new terminal
- Open VS Code by entering
code .
in the terminal - Press Run/Debug (F5) to run in the debugger
- Use same approach above to start an orchestration instance and check its status.
In the root folder (Durable-Functions-Order-Processing) use the Azure Developer CLI (azd) to provision a new resource group with the environment name you provide and all the resources for the sample by running:
azd up
Once the deployment is done, inspect the new resource group. The Flex Consumption function app and plan, storage, App Insights, and networking related resources have been created and configured:
Because Durable Functions requires access to Azure Storage Blob, Table, and Queue, the associated networking resources such as private endpoints, link, etc. are created for each of those.
Flex Consumption has an always ready feature that allows users to specify the number of compute instances that are always running to minimize cold start. This sample sets always ready instance to 1 for the "durable" group.
The property maxQueuePollingInterval
is also set to 1 second to decrease message processing latencies. Note, however, that lower values of maxQueuePollingInterval
can result in higher storage costs because of increased storage transactions.
-
Use this Function CLI command to quickly find the function app url:
func azure functionapp list-functions <APP_NAME> --show-keys
The url should look something like this: https://func-processor-abcdefgxzy.azurewebsites.net/api/OrderProcessingOrchestration_HttpStart.
-
Open up a browser and go that url to trigger the start of an orchestration instance.
-
Go to the
statusQueryGetUri
to see the status of your orchestration instance. It should show "Running" at the beginning, but change to "Completed" after a few seconds if you refresh the page, just like shown above.
When you no longer need the resources created in this sample, run the following command to delete the Azure resources:
azd down
For more information on Durable Functions, the new Flex Consumption plan, and VNet integration, see the following resources: