Azure Functions - 如何从函数中获取调用 ID?

Azure Functions - how to obtain invocation ID from within the function?

我正在尝试 return Azure 函数的调用 ID,类似于 WebJob 在 HTTP 位置 header 中发送回 WebJob 运行 ID。这样我的函数的调用者就可以定期检查状态以了解它何时完成。

我发现我必须将此 ID 添加到响应 object 中,我想我需要从函数中的某些上下文 object 中检索它。这是因为当我在 https://functionapp.scm.azurewebsites.net/azurejobs/#/functions/invocations/a-long-guid 访问函数 UI 时 我看到一个名为 _context 的变量和调用 ID。但是,我似乎无法在我的函数中访问名为 context、_context 等的变量。

您可以通过向函数的方法添加该类型的参数来绑定到 ExecutionContext(例如 Run(..., ExecutionContext context))。

该类型公开了一个 InvocationId 属性,它将为您提供所需的信息。