Getting Started with Azure Durable Functions

Durable Functions are a serverless extension of Azure Functions that allow developers to build complex, long-running workflows in a simple and efficient manner. With Durable Functions, you can easily create stateful functions that can be orchestrated to achieve a specific business goal. In this article, we'll discuss how to create and use Durable Functions using C#.

Getting Started with Durable Functions

Before building Durable Functions, you need to set up your development environment. You will need to have Visual Studio installed and the Azure Functions and Azure Storage Emulator extensions. Once you have those installed, you can create a new Azure Functions project in Visual Studio and select the Durable Functions template.

Creating a Durable Function

To create a Durable Function, you must create a new class that inherits from the DurableOrchestrationClientBase class. This class provides methods for creating and managing durable orchestrations. Here's an example:

using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;

namespace DurableFunctionDemo
{
    public class MyOrchestratorFunction : DurableOrchestrationClientBase
    {
        [FunctionName("MyOrchestratorFunction")]
        public async Task<string> RunOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext context)
        {
            var result = await context.CallActivityAsync<string>("MyActivityFunction", "Hello, world!");
            return result;
        }

        [FunctionName("MyActivityFunction")]
        public string RunActivity([ActivityTrigger] string message)
        {
            return message;
        }
    }
}

In this example, we've created a new class called MyOrchestratorFunction that inherits from DurableOrchestrationClientBase. This class contains two methods: RunOrchestrator and RunActivity. The RunOrchestrator method is marked with the [OrchestrationTrigger] attribute, which indicates that it will be the entry point for the orchestration. The method takes an IDurableOrchestrationContext object as its parameter, which provides access to the orchestration context.

Inside the RunOrchestrator method, we call the MyActivityFunction activity using the CallActivityAsync method. This method takes the name of the activity function and any parameters that need to be passed to it.

The MyActivityFunction method is marked with the [ActivityTrigger] attribute, which indicates that it will be an activity function. The method takes a string parameter and returns the same string. This simple activity function echoes back the message that was passed to it.

Starting a Durable Function

To start a Durable Function, you need to use the DurableOrchestrationClient class. Here's an example:

using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;

namespace DurableFunctionDemo
{
    public static class MyFunction
    {
        [FunctionName("MyFunction")]
        public static async Task<string> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [DurableClient] IDurableOrchestrationClient starter)
        {
            string instanceId = await starter.StartNewAsync("MyOrchestratorFunction", null);
            return starter.CreateCheckStatusResponse(req, instanceId).Output;
        }
    }
}

In this example, we've created a new function called MyFunction triggered by an HTTP request. The function takes an HttpRequest object as its parameter, which provides access to the HTTP request context. We've also added the [DurableClient] attribute to the IDurableOrchest.


Similar Articles