3

I'm writing a Durable Function and need to generate some GUIDs. According to the documentation, you need to take extra care with this because GUID generation is not deterministic. When the orchestration is replayed, it should use the same GUID, and the workaround described in the documentation is to

Use NewGuid in .NET or newGuid in JavaScript to safely generate random GUIDs.

I assume they mean the static method Guid.NewGuid(), yet when I use it in my code, like this:

[FunctionName("Orchestration")]
public static async Task Orchestration([OrchestrationTrigger] IDurableOrchestrationContext context, ILogger logger) {
    var guid = Guid.NewGuid();

I get a compiler warning:

Warning DF0102: 'Guid.NewGuid' violates the orchestrator deterministic code constraint. (DF0102)

and when I run the function, I see that it produces a different GUID when replaying, so it's definitely not deterministic. I know I can write an activity function to generate a GUID, but this seems a bit overkill if there's dedicated support for this. This GitHub comment mentions it has been released in v1.7.0 (I'm using v2.3.1).

Glorfindel
  • 21,988
  • 13
  • 81
  • 109

1 Answers1

3

While writing this question, I realized what my problem was. It's this assumption:

I assume they mean the static method Guid.NewGuid()

It turns out the IDurableOrchestrationContext interface has a NewGuid() method as well, which

Creates a new GUID that is safe for replay within an orchestration or operation.

[FunctionName("Orchestration")]
public static async Task Orchestration([OrchestrationTrigger] IDurableOrchestrationContext context, ILogger logger) {
    var guid = context.NewGuid();
Glorfindel
  • 21,988
  • 13
  • 81
  • 109