Durable Functions 中的任务中心 (Azure Functions)Task hubs in Durable Functions (Azure Functions)

Durable Functions 中的任务中心是用于业务流程的 Azure 存储资源的逻辑容器。A task hub in Durable Functions is a logical container for Azure Storage resources that are used for orchestrations. 只有当业务流程协调程序函数与活动函数属于同一任务中心时,它们才能彼此进行交互。Orchestrator and activity functions can only interact with each other when they belong to the same task hub.

如果多个函数应用共享存储帐户,则必须使用单独的任务中心名称配置每个函数应用 。If multiple function apps share a storage account, each function app must be configured with a separate task hub name. 一个存储帐户可以包含多个任务中心。A storage account can contain multiple task hubs. 下图说明了在共享和专用存储帐户中每个函数应用有一个任务中心。The following diagram illustrates one task hub per function app in shared and dedicated storage accounts.

说明共享和专用存储帐户的关系图。

Azure 存储资源Azure Storage resources

任务中心包含以下存储资源:A task hub consists of the following storage resources:

  • 一个或多个控制队列。One or more control queues.
  • 一个工作项队列。One work-item queue.
  • 一个历史记录表。One history table.
  • 一个实例表。One instances table.
  • 一个包含一个或多个租用 blob 的存储容器。One storage container containing one or more lease blobs.
  • 包含大的消息有效负载(如果适用)的存储容器。A storage container containing large message payloads, if applicable.

当业务流程协调程序实体或活动函数运行时或调度它们运行时,将自动在默认 Azure 存储帐户中创建所有这些资源。All of these resources are created automatically in the default Azure Storage account when orchestrator, entity, or activity functions run or are scheduled to run. 性能和缩放一文介绍了如何使用这些资源。The Performance and Scale article explains how these resources are used.

任务中心名称Task hub names

任务中心由 host.json 文件中声明的名称标识,如以下示例所示:Task hubs are identified by a name that is declared in the host.json file, as shown in the following example:

host.json (Functions 2.0)host.json (Functions 2.0)

{
  "version": "2.0",
  "extensions": {
    "durableTask": {
      "hubName": "MyTaskHub"
    }
  }
}

host.json (Functions 1.x)host.json (Functions 1.x)

{
  "durableTask": {
    "hubName": "MyTaskHub"
  }
}

还可以使用应用设置配置任务中心,如以下 host.json 示例文件所示:Task hubs can also be configured using app settings, as shown in the following host.json example file:

host.json (Functions 1.0)host.json (Functions 1.0)

{
  "durableTask": {
    "hubName": "%MyTaskHub%"
  }
}

host.json (Functions 2.0)host.json (Functions 2.0)

{
  "version": "2.0",
  "extensions": {
    "durableTask": {
      "hubName": "%MyTaskHub%"
    }
  }
}

任务中心名称将设置为 MyTaskHub 应用设置的值。The task hub name will be set to the value of the MyTaskHub app setting. 以下 local.settings.json 演示了如何将 MyTaskHub 设置定义为 samplehubnameThe following local.settings.json demonstrates how to define the MyTaskHub setting as samplehubname:

{
  "IsEncrypted": false,
  "Values": {
    "MyTaskHub" : "samplehubname"
  }
}

以下代码是一个预编译的 C# 示例,说明如何编写一个特定函数,该函数使用业务流程客户端绑定来处理配置为应用设置的任务中心:The following code is a precompiled C# example of how to write a function that uses the orchestration client binding to work with a task hub that is configured as an App Setting:

C#C#

[FunctionName("HttpStart")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, methods: "post", Route = "orchestrators/{functionName}")] HttpRequestMessage req,
    [OrchestrationClient(TaskHub = "%MyTaskHub%")] IDurableOrchestrationClient starter,
    string functionName,
    ILogger log)
{
    // Function input comes from the request content.
    dynamic eventData = await req.Content.ReadAsAsync<object>();
    string instanceId = await starter.StartNewAsync(functionName, eventData);

    log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

    return starter.CreateCheckStatusResponse(req, instanceId);
}

Note

前面的 C# 示例适用于 Durable Functions 2.x。The previous C# example is for Durable Functions 2.x. 对于 Durable Functions 1.x,必须使用 DurableOrchestrationContext 而不是 IDurableOrchestrationContextFor Durable Functions 1.x, you must use DurableOrchestrationContext instead of IDurableOrchestrationContext. 有关版本之间差异的详细信息,请参阅 Durable Functions 版本一文。For more information about the differences between versions, see the Durable Functions versions article.

JavaScriptJavaScript

function.json 文件中的任务中心属性通过应用设置进行设置:The task hub property in the function.json file is set via App Setting:

{
    "name": "input",
    "taskHub": "%MyTaskHub%",
    "type": "orchestrationClient",
    "direction": "in"
}

任务中心名称必须以字母开头且只能包含字母和数字。Task hub names must start with a letter and consist of only letters and numbers. 如果未指定,则会使用默认的任务中心名称,如下表所示:If not specified, a default task hub name will be used as shown in the following table:

Durable 扩展版本Durable extension version 默认的任务中心名称Default task hub name
2.x2.x 在 Azure 中部署时,任务中心名称派生自函数应用 的名称。When deployed in Azure, the task hub name is derived from the name of the function app. 在 Azure 外部运行时,默认的任务中心名称为 TestHubNameWhen running outside of Azure, the default task hub name is TestHubName.
1.x1.x 所有环境的默认任务中心名称为 DurableFunctionsHubThe default task hub name for all environments is DurableFunctionsHub.

有关扩展版本之间差异的详细信息,请参阅 Durable Functions 版本一文。For more information about the differences between extension versions, see the Durable Functions versions article.

Note

当共享存储帐户中有多个任务中心时,名称用于将一个任务中心与其他任务中心区分开来。The name is what differentiates one task hub from another when there are multiple task hubs in a shared storage account. 如果有多个函数应用共享一个共享存储帐户,则必须在 host.json 文件中为每个任务中心显式配置不同的名称。If you have multiple function apps sharing a shared storage account, you must explicitly configure different names for each task hub in the host.json files. 否则多个函数应用会相互竞争消息,这可能会导致未定义的行为,例如业务流程意外“卡”在 PendingRunning 状态。Otherwise the multiple function apps will compete with each other for messages, which could result in undefined behavior, including orchestrations getting unexpectedly "stuck" in the Pending or Running state.

后续步骤Next steps