Question

Difficulty: MediumImplement Durable Functions

You are developing an Azure Durable Function in C# (.NET Isolated) to process user registration requests. The orchestrator function must generate a unique correlation ID for tracking and retrieve the current timestamp to record when the process started.

You need to ensure that the orchestrator code remains deterministic and adheres to Durable Functions execution constraints.

Which code segment should you use inside the orchestrator function?

  1. A
    Guid correlationId = Guid.NewGuid(); DateTime startTimestamp = DateTime.UtcNow;
  2. B
    Guid correlationId = context.CreateGuid(); DateTime startTimestamp = DateTime.UtcNow;
  3. Guid correlationId = context.CreateGuid(); DateTime startTimestamp = context.CurrentUtcDateTime;Answer
  4. D
    Guid correlationId = Guid.NewGuid(); DateTime startTimestamp = context.CurrentUtcDateTime;

Answer

Use context.CreateGuid() to generate the correlation ID and context.CurrentUtcDateTime to retrieve the timestamp.
The option utilizing context.CreateGuid() and context.CurrentUtcDateTime is correct because it adheres to the determinism constraints of Durable Functions. In Azure Durable Functions, orchestrator functions are replayed to rebuild their state. Therefore, code inside an orchestrator must be deterministic. Standard APIs like Guid.NewGuid() and DateTime.UtcNow return different values on every execution, causing the orchestration to fail with a non-deterministic workflow error. The TaskOrchestrationContext provides deterministic alternatives: CreateGuid() generates a GUID that is saved and replayed consistently, and CurrentUtcDateTime returns the timestamp of when the orchestrator was scheduled, which is also replayed consistently.

Step-by-Step Solution

1
Analyze the determinism constraints of Azure Durable Functions orchestrator functions.
Identify that APIs returning different values on execution replay (such as generating GUIDs or retrieving system time) cannot be used directly inside the orchestrator.
Orchestrators must run deterministically to rebuild state via execution replay.
2
Identify the deterministic equivalents provided by the TaskOrchestrationContext object in C# (.NET Isolated).
Determine that context.CreateGuid() replaces Guid.NewGuid(), and context.CurrentUtcDateTime replaces DateTime.UtcNow.
These context APIs record their values in the orchestration history during the first execution and return the identical recorded values during subsequent replays.

Key Concept

Orchestrator code determinism and using context-specific APIs for GUIDs and timestamps.
Rate this question