Getting Started with Azure Durable Functions

This guide will walk you through the initial steps to create and run your first Azure Durable Function. Durable Functions extend Azure Functions by enabling you to write stateful functions in a serverless environment.

Prerequisites

Before you begin, ensure you have the following installed:

If you don't have them, follow the official Azure Functions installation guide.

Step 1: Create a New Azure Functions Project

Open your terminal or command prompt and run the following command to create a new Azure Functions project:

func init MyDurableApp --worker-runtime dotnet
cd MyDurableApp

Step 2: Add a Durable Functions Extension

You need to add the Durable Functions extension to your project. Navigate to your project directory and run:

dotnet add package Microsoft.Azure.WebJobs.Extensions.DurableTask --version 2.0.0

Note: The version number might vary; check the latest stable version.

Step 3: Create Your First Orchestrator Function

Now, let's create a simple orchestrator function. Use the Azure Functions Core Tools CLI:

func new --name MyOrchestrator --template DurableClient --language CSharp

This command creates a starter orchestrator function. You can modify the generated code to define your workflow.

Orchestrator Code Example

Open the `MyOrchestrator.cs` file. It will look something like this:

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;

namespace MyDurableApp
{
    public static class MyOrchestrator
    {
        [FunctionName("MyOrchestrator")]
        public static async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context)
        {
            // Replace with your actual workflow logic
            DateTime nextRunningTime = context.CurrentUtcDateTime.AddSeconds(5);
            await context.CreateTimer(nextRunningTime, System.Threading.CancellationToken.None);
            context.SetOutput($"Hello from durable functions! The current time is {context.CurrentUtcDateTime}.");
        }

        [FunctionName("HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
            [DurableClient] IDurableClient starter)
        {
            // Function input comes from the request content.
            string instanceId = await starter.StartNewAsync("MyOrchestrator", null);

            Console.WriteLine($"Started orchestration with ID = '{instanceId}'.");

            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }
}

Step 4: Run Your Durable Function Locally

You can now run your function app locally. From your project's root directory, run:

func start

The output will show the endpoints for your HTTP-triggered function. Look for a URL like:

HttpStart: [GET,POST] http://localhost:7071/api/HttpStart

Step 5: Trigger Your Orchestration

Open your web browser or use a tool like cURL or Postman to make a request to the `HttpStart` endpoint. For example:

curl http://localhost:7071/api/HttpStart

You should receive a response containing status URLs for your orchestration. You can use the check-status URL to see the progress and output of your orchestrator.

Important: For local development, Durable Functions uses a built-in storage provider. When deploying to Azure, you'll need to configure a proper storage account (e.g., Azure Storage).

Next Steps

Congratulations! You've just run your first Durable Function. Here are some resources to continue learning: