Durable Functions 类型和功能Durable Functions types and features

Durable Functions 是 Azure Functions 的一个扩展。Durable Functions is an extension of Azure Functions. 可将 Azure Functions 用于函数执行的有状态业务流程。You can use Durable Functions for stateful orchestration of function execution. 持久函数应用是由不同 Azure 函数构成的解决方案。A durable function app is a solution that's made up of different Azure functions. 函数可在持久函数业务流程中扮演不同的角色。Functions can play different roles in a durable function orchestration.

Azure Functions 中目前有四种持久函数类型:活动、业务流程协调程序、实体和客户端。There are currently four durable function types in Azure Functions: activity, orchestrator, entity, and client. 此部分的其余内容详细介绍业务流程中涉及的函数类型。The rest of this section goes into more details about the types of functions involved in an orchestration.

业务流程协调程序函数Orchestrator functions

业务流程协调程序函数描述操作的执行方式和操作的执行顺序。Orchestrator functions describe how actions are executed and the order in which actions are executed. 业务流程协调程序函数描述代码(C# 或 JavaScript)中的业务流程,如 Durable Functions 应用程序模式中所示。Orchestrator functions describe the orchestration in code (C# or JavaScript) as shown in Durable Functions application patterns. 业务流程可以包含许多不同类型的操作,包括活动函数子业务流程等待外部事件HTTP计时器An orchestration can have many different types of actions, including activity functions, sub-orchestrations, waiting for external events, HTTP, and timers. 业务流程协调程序函数也可以与实体函数交互。Orchestrator functions can also interact with entity functions.

备注

业务流程协调程序函数使用常规代码编写,但对于如何编写代码存在严格的要求。Orchestrator functions are written using ordinary code, but there are strict requirements on how to write the code. 具体说来,业务流程协调程序函数代码必须是确定性的Specifically, orchestrator function code must be deterministic. 如果没有遵循这些确定性要求,则可能导致业务流程协调程序函数无法正确运行。Failing to follow these determinism requirements can cause orchestrator functions to fail to run correctly. 若要详细了解这些要求以及如何应对它们,可参阅代码约束主题。Detailed information on these requirements and how to work around them can be found in the code constraints topic.

若要更详细地了解业务流程协调程序函数及其功能,请参阅持久业务流程一文。For more detailed information on orchestrator functions and their features, see the Durable orchestrations article.

活动函数Activity functions

活动函数是持久函数业务流程中的基本工作单元。Activity functions are the basic unit of work in a durable function orchestration. 活动函数是在过程中协调的函数和任务。Activity functions are the functions and tasks that are orchestrated in the process. 例如,可以创建一个业务流程协调程序函数来处理订单。For example, you might create an orchestrator function to process an order. 任务涉及到检查库存、向客户收费和创建发货单。The tasks involve checking the inventory, charging the customer, and creating a shipment. 每个任务都是一个单独的活动函数。Each task would be a separate activity function. 这些活动函数可以顺序执行,也可以并行执行,或按二者的某种组合来执行。These activity functions may be executed serially, in parallel, or some combination of both.

与业务流程协调程序函数不同,活动函数对于可在其中执行的工作类型没有限制。Unlike orchestrator functions, activity functions aren't restricted in the type of work you can do in them. 活动函数频繁用于进行网络调用,或运行 CPU 密集型操作。Activity functions are frequently used to make network calls or run CPU intensive operations. 活动函数还可以将数据返回到业务流程协调程序函数。An activity function can also return data back to the orchestrator function. Durable Task Framework 可保证在业务流程执行期间每个调用的活动函数至少执行一次。 The Durable Task Framework guarantees that each called activity function will be executed at least once during an orchestration's execution.

备注

由于活动函数只保证至少执行一次, 因此建议你尽可能将活动函数逻辑设置为幂等。 Because activity functions only guarantee at least once execution, we recommend you make your activity function logic idempotent whenever possible.

使用活动触发器定义活动函数。Use an activity trigger to define an activity function. .NET 函数以参数的形式接收 DurableActivityContext.NET functions receive a DurableActivityContext as a parameter. 还可以将触发器绑定到任何其他可进行 JSON 序列化的对象,将输入传递给函数。You can also bind the trigger to any other JSON-serializeable object to pass in inputs to the function. 在 JavaScript 中,可以通过 context.bindings 对象中的 <activity trigger binding name> 属性访问输入。In JavaScript, you can access an input via the <activity trigger binding name> property on the context.bindings object. 活动函数只能将单个值传递给它们。Activity functions can only have a single value passed to them. 若要传递多个值,必须使用元组、数组或复杂类型。To pass multiple values, you must use tuples, arrays, or complex types.

备注

只能从业务流程协调程序函数触发活动函数。You can trigger an activity function only from an orchestrator function.

实体函数Entity functions

实体函数定义用于读取和更新较小状态片段的操作。Entity functions define operations for reading and updating small pieces of state. 我们通常将此类有状态实体称为“持久实体”。 We often refer to these stateful entities as durable entities. 与业务流程协调程序函数类似,实体函数是具有特殊触发器类型“实体触发器”的函数。 Like orchestrator functions, entity functions are functions with a special trigger type, entity trigger. 它们也可以从客户端函数或业务流程协调程序函数调用。They can also be invoked from client functions or from orchestrator functions. 与业务流程协调程序函数不同,实体函数没有任何特定的代码约束。Unlike orchestrator functions, entity functions do not have any specific code constraints. 实体函数还会显式管理状态,而不是通过控制流隐式表示状态。Entity functions also manage state explicitly rather than implicitly representing state via control flow.

备注

实体函数和相关功能仅在 Durable Functions 2.0 及更高版本中可用。Entity functions and related functionality is only available in Durable Functions 2.0 and above.

有关实体函数的详细信息,请参阅持久实体一文。For more information about entity functions, see the Durable Entities article.

客户端函数Client functions

业务流程协调程序函数通过业务流程触发器绑定触发,实体函数通过实体触发器绑定触发。Orchestrator functions are triggered by an orchestration trigger binding and entity functions are triggered by an entity trigger binding. 这两个触发器在生效时,会响应在任务中心排队的消息。Both of these triggers work by reacting to messages that are enqueued into a task hub. 若要传递这些消息,主要方式是在某个客户端函数中使用业务流程协调程序客户端绑定实体客户端绑定The primary way to deliver these messages is by using an orchestrator client binding or an entity client binding from within a client function. 任何非业务流程协调程序函数都可以是客户端函数。 Any non-orchestrator function can be a client function. 例如,可以从 HTTP 触发的函数、Azure 事件中心触发的函数等函数触发业务流程协调程序。如果某个函数使用持久客户端输出绑定,则可将其视为客户端函数。 For example, You can trigger the orchestrator from an HTTP-triggered function, an Azure Event Hub triggered function, etc. What makes a function a client function is its use of the durable client output binding.

备注

与其他函数类型不同,业务流程协调程序函数和实体函数不能在 Azure 门户中使用按钮直接触发。Unlike other function types, orchestrator and entity functions cannot be triggered directly using the buttons in the Azure Portal. 若要在 Azure 门户中测试某个业务流程协调程序函数或实体函数,必须改为运行一个客户端函数,该函数会在实现过程中启动业务流程协调程序函数或实体函数。 If you want to test an orchestrator or entity function in the Azure Portal, you must instead run a client function that starts an orchestrator or entity function as part of its implementation. 若要获取最简单的测试体验,建议使用手动触发器函数。 For the simplest testing experience, a manual trigger function is recommended.

除了触发业务流程协调程序函数或实体函数,还可以使用持久客户端绑定与正在运行的业务流程和实体进行交互。 In addition to triggering orchestrator or entity functions, the durable client binding can be used to interact with running orchestrations and entities. 例如,业务流程可以查询、可以终止,还可以引发事件。For example, orchestrations can be queried, terminated, and can have events raised to them. 若要详细了解如何管理业务流程和实体,请参阅实例管理一文。For more information on managing orchestrations and entities, see the Instance management article.

后续步骤Next steps

若要开始体验,请在 C#JavaScript 中创建第一个持久函数。To get started, create your first durable function in C# or JavaScript.