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?
- AGuid correlationId = Guid.NewGuid(); DateTime startTimestamp = DateTime.UtcNow;
- BGuid correlationId = context.CreateGuid(); DateTime startTimestamp = DateTime.UtcNow;
- Guid correlationId = context.CreateGuid(); DateTime startTimestamp = context.CurrentUtcDateTime;Answer
- DGuid 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
Key Concept
Orchestrator code determinism and using context-specific APIs for GUIDs and timestamps.