Question

Difficulty: MediumImplement Durable Functions

You are developing an Azure Durable Functions application in C# using the .NET Isolated worker model. You write the following orchestrator function to manage an order processing workflow:

csharp
[Function("ProcessOrderOrchestrator")]
public static async Task RunOrchestrator(
[OrchestrationTrigger] TaskOrchestrationContext context)
{
var orderId = context.GetInput<string>();

var status = await context.CallActivityAsync<string>("CheckStatus", orderId);

var trackingId = Guid.NewGuid().ToString();

await context.CallActivityAsync("ProcessPayment", new { OrderId = orderId, TrackingId = trackingId });
}

Which line of code in this orchestrator function violates the determinism constraints of Durable Functions?

  1. A
    var orderId = context.GetInput<string>();
  2. B
    var status = await context.CallActivityAsync<string>("CheckStatus", orderId);
  3. var trackingId = Guid.NewGuid().ToString();Answer
  4. D
    await context.CallActivityAsync("ProcessPayment", new { OrderId = orderId, TrackingId = trackingId });

Answer

The statement generating the tracking ID using Guid.NewGuid()
The statement `var trackingId = Guid.NewGuid().ToString();` violates the determinism constraints of Durable Functions orchestrator functions. Orchestrator functions must be deterministic because they are replayed from the beginning of the execution history to rebuild the state of the orchestration. Generating a new GUID via `Guid.NewGuid()` produces a different value on every replay, leading to non-deterministic execution paths and runtime errors. Instead, the orchestrator should use `context.NewGuid()` to safely generate a random identifier that yields the same value during replays.

Step-by-Step Solution

1
Analyze the orchestrator function for non-deterministic APIs or operations.
Identify that Guid.NewGuid() is invoked directly in the orchestrator.
Orchestrator functions must be deterministic because their execution is replayed to rebuild state.
2
Identify replay-safe alternatives for generating identifiers in Durable Functions.
The TaskOrchestrationContext provides the NewGuid() API to safely generate GUIDs deterministically during replays.
Using context.NewGuid() allows the framework to return the same GUID during replay, preserving determinism.

Key Concept

Durable Functions Orchestrator Code Constraints and Determinism
Rate this question