如何在 Azure Durable Function 中安全地生成 GUID?

How can I safely generate GUIDs within an Azure Durable Function?

我正在编写 Durable Function 并且需要生成一些 GUID。根据 the documentation,您需要格外小心,因为 GUID 生成是不确定的。重播编排时,应该使用相同的GUID,文档中描述的解决方法是

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

我假设它们指的是 static method Guid.NewGuid(),但是当我在我的代码中使用它时,就像这样:

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

我收到编译器警告:

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

并且当我 运行 函数时,我看到它在重播时产生不同的 GUID,因此它绝对不是确定性的。我知道我可以编写一个 activity 函数来生成 GUID,但如果有专门的支持,这似乎有点过分了。 This GitHub comment 提到它已在 v1.7.0 中发布(我正在使用 v2.3.1)。

在写这个问题的时候,我意识到我的问题是什么。这是这个假设:

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

原来 IDurableOrchestrationContext 接口也有一个 NewGuid() method

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();