Skip to content

This repository contains the implementation of an order processing workflow with Durable Functions in C#. The sample is deployed to Azure Functions Flex Consumption using the Azure Developer CLI (azd) and is configured with managed identity as the authentication mechanism.

License

Notifications You must be signed in to change notification settings

Azure-Samples/Durable-Functions-Order-Processing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Order processing workflow with Durable Functions

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!

Run in your local environment

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:

Prerequisites

  • .NET 8 SDK

  • Azure Functions Core Tools

  • 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"
      }
    }

Using Azure Functions Core Tools (CLI)

  1. Open a new terminal and do the following:
cd OrderProcessor
func start
  1. 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>"
}
  1. 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"
}

Using Visual Studio

  1. Open project using Visual Studio 2022 or later.
  2. Press Run/F5 to run in the debugger
  3. Use same approach above to start an orchestration instance and check its status.

Using Visual Studio Code

  1. Open this folder in a new terminal
  2. Open VS Code by entering code . in the terminal
  3. Press Run/Debug (F5) to run in the debugger
  4. Use same approach above to start an orchestration instance and check its status.

Provision the solution on Azure

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

Inspect the solution (optional)

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: Screenshot of resources created by the bicept template

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.

Test the solution

  1. 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.

  2. Open up a browser and go that url to trigger the start of an orchestration instance.

  3. 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.

Clean up resources

When you no longer need the resources created in this sample, run the following command to delete the Azure resources:

azd down

Resources

For more information on Durable Functions, the new Flex Consumption plan, and VNet integration, see the following resources:

About

This repository contains the implementation of an order processing workflow with Durable Functions in C#. The sample is deployed to Azure Functions Flex Consumption using the Azure Developer CLI (azd) and is configured with managed identity as the authentication mechanism.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •