Azure Functions 触发器和绑定概念Azure Functions triggers and bindings concepts

本文概要介绍有关函数触发器和绑定的概念。In this article you learn the high-level concepts surrounding functions triggers and bindings.

触发器是导致函数运行的因素。Triggers are what cause a function to run. 触发器定义函数的调用方式,一个函数必须刚好有一个触发器。A trigger defines how a function is invoked and a function must have exactly one trigger. 触发器具有关联的数据,这些数据通常作为函数的有效负载提供。Triggers have associated data, which is often provided as the payload of the function.

绑定到函数是以声明方式将另一个资源连接到该函数的一种方式;绑定可以输入绑定和/或输出绑定的形式进行连接。 Binding to a function is a way of declaratively connecting another resource to the function; bindings may be connected as input bindings, output bindings, or both. 绑定中的数据作为参数提供给函数。Data from bindings is provided to the function as parameters.

可根据需要,混合搭配不同的绑定。You can mix and match different bindings to suit your needs. 绑定是可选的,一个函数可以有一个或多个输入绑定和/或输出绑定。Bindings are optional and a function might have one or multiple input and/or output bindings.

使用触发器和绑定可以避免对其他服务进行硬编码访问。Triggers and bindings let you avoid hardcoding access to other services. 函数接收函数参数中的数据(例如,队列消息内容)。Your function receives data (for example, the content of a queue message) in function parameters. 使用函数的返回值发送数据(例如,用于创建队列消息)。You send data (for example, to create a queue message) by using the return value of the function.

以下示例演示如何实现不同的函数。Consider the following examples of how you could implement different functions.

示例方案Example scenario 触发器Trigger 输入绑定Input binding 输出绑定Output binding
新的队列消息抵达,此时会运行一个函数来写入到另一个队列。A new queue message arrives which runs a function to write to another queue. 队列*Queue* None 队列*Queue*
计划的作业读取 Blob 存储内容,并创建新的 Cosmos DB 文档。A scheduled job reads Blob Storage contents and creates a new Cosmos DB document. TimerTimer Blob 存储Blob Storage Cosmos DBCosmos DB
事件网格用于读取 Blob 存储中的映像以及 Cosmos DB 中的文档,以发送电子邮件。The Event Grid is used to read an image from Blob Storage and a document from Cosmos DB to send an email. 事件网格Event Grid Blob 存储和 Cosmos DBBlob Storage and Cosmos DB SendGridSendGrid
一个 Webhook,它使用 Microsoft Graph 来更新 Excel 工作表。A webhook that uses Microsoft Graph to update an Excel sheet. HTTPHTTP None Microsoft GraphMicrosoft Graph

* 表示不同的队列* Represents different queues

这些示例并不详尽,旨在演示如何同时使用触发器和绑定。These examples are not meant to be exhaustive, but are provided to illustrate how you can use triggers and bindings together.

触发器和绑定的定义Trigger and binding definitions

触发器和绑定的定义根据开发语言的不同而异。Triggers and bindings are defined differently depending on the development language.

语言Language 触发器和绑定的配置方式...Triggers and bindings are configured by...
C# 类库C# class library      使用 C# 特性修饰方法和参数     decorating methods and parameters with C# attributes
JavaJava      使用 Java 注释修饰方法和参数     decorating methods and parameters with Java annotations
JavaScript/PowerShell/TypeScriptJavaScript/PowerShell/TypeScript      更新 function.json架构     updating function.json (schema)

对于依赖 function.json 的语言,门户在“集成”选项卡中提供了用于添加绑定的 UI。还可以在门户中函数的“代码 + 测试”选项卡中直接编辑该文件。For languages that rely on function.json, the portal provides a UI for adding bindings in the Integration tab. You can also edit the file directly in the portal in the Code + test tab of your function. 使用 Visual Studio Code,可以通过一套方便的提示轻松地将绑定添加到 function.json 文件Visual Studio Code lets you easily add a binding to a function.json file by following a convenient set of prompts.

在 .NET 和 Java 中,参数类型定义了输入数据的数据类型。In .NET and Java, the parameter type defines the data type for input data. 例如,使用 string 绑定到队列触发器的文本、一个要读取为二进制内容的字节数组,以及一个要反序列化为对象的自定义类型。For instance, use string to bind to the text of a queue trigger, a byte array to read as binary, and a custom type to de-serialize to an object. 由于 .NET 类库函数和 Java 函数不依赖 function.json 来绑定定义,所以在门户中不能对它们进行创建和编辑。Since .NET class library functions and Java functions don't rely on function.json for binding definitions, they can't be created and edited in the portal. C# 门户编辑基于C# 脚本,该脚本使用 function.json 而不是特性。C# portal editing is based on C# script, which uses function.json instead of attributes.

若要详细了解如何将绑定添加到现有函数,请参阅使用绑定将函数连接到 Azure 服务To learn more about how to adding bindings to existing functions, see Connect functions to Azure services using bindings.

对于动态键入的语言(如 JavaScript),请在 function.json 文件中使用 dataType 属性。For languages that are dynamically typed such as JavaScript, use the dataType property in the function.json file. 例如,若要以二进制格式读取 HTTP 请求的内容,将 dataType 设置为 binaryFor example, to read the content of an HTTP request in binary format, set dataType to binary:

    "dataType": "binary",
    "type": "httpTrigger",
    "name": "req",
    "direction": "in"

dataType 的其他选项是 streamstringOther options for dataType are stream and string.

绑定方向Binding direction

所有触发器和绑定在 function.json 文件中都有一个 direction 属性:All triggers and bindings have a direction property in the function.json file:

  • 对于触发器,方向始终为 inFor triggers, the direction is always in
  • 输入和输出绑定使用 inoutInput and output bindings use in and out
  • 某些绑定支持特殊方向 inoutSome bindings support a special direction inout. 如果使用 inout,则只能通过门户中的“集成”选项卡使用“高级编辑器”。 If you use inout, only the Advanced editor is available via the Integrate tab in the portal.

使用类库中的特性来配置触发器和绑定时,方向在特性构造函数中提供或推断自参数类型。When you use attributes in a class library to configure triggers and bindings, the direction is provided in an attribute constructor or inferred from the parameter type.

向函数添加绑定Add bindings to a function

可以使用输入或输出绑定将函数连接到其他服务。You can connect your function to other services by using input or output bindings. 通过向函数添加绑定的具体定义来添加绑定。Add a binding by adding its specific definitions to your function. 若要了解如何操作,请参阅向 Azure Functions 中的现有函数添加绑定To learn how, see Add bindings to an existing function in Azure Functions.

支持的绑定Supported bindings

下表显示了 Azure Functions 运行时的主版本支持的绑定:This table shows the bindings that are supported in the major versions of the Azure Functions runtime:

类型Type 1.x1.x 2.x 及更高版本12.x and higher1 触发器Trigger 输入Input 输出Output
Blob 存储Blob storage
Azure Cosmos DBAzure Cosmos DB
事件网格Event Grid
事件中心Event Hubs
HTTP 和 WebhookHTTP & webhooks
IoT 中心IoT Hub
移动应用Mobile Apps
通知中心Notification Hubs
队列存储Queue storage
服务总线Service Bus
表存储Table storage

1 从版本 2.x 运行时开始,除了 HTTP 和 Timer 以外,所有绑定都必须注册。1 Starting with the version 2.x runtime, all bindings except HTTP and Timer must be registered. 请参阅注册绑定扩展See Register binding extensions.

2 消耗计划中不支持触发器。2 Triggers aren't supported in the Consumption plan. 需要运行时驱动的触发器Requires runtime-driven triggers.

3 仅支持 Kubernetes、IoT Edge 和其他自托管模式。3 Supported only in Kubernetes, IoT Edge, and other self-hosted modes only.

有关哪些绑定处于预览状态或已批准在生产环境中使用的信息,请参阅支持的语言For information about which bindings are in preview or are approved for production use, see Supported languages.

绑定代码示例Bindings code examples

使用下表查找特定绑定类型的示例,这些示例演示如何在函数中使用绑定。Use the following table to find examples of specific binding types that show you how to work with bindings in your functions. 首先,选择与你的项目相对应的语言选项卡。First, choose the language tab that corresponds to your project.

服务Service 示例Examples 示例Samples
Blob 存储Blob storage 触发器Trigger
Azure Cosmos DBAzure Cosmos DB 触发器Trigger
事件网格Event Grid 触发器Trigger
事件中心Event Hubs 触发器Trigger
IoT 中心IoT Hub 触发器Trigger
HTTPHTTP 触发器Trigger 链接Link
队列存储Queue storage 触发器Trigger
RabbitMQRabbitMQ 触发器Trigger
SendGridSendGrid 输出Output
服务总线Service Bus 触发器Trigger
SignalRSignalR 触发器Trigger
表存储Table storage 输入Input
TimerTimer 触发器Trigger 链接Link

自定义绑定Custom bindings

可以创建自定义输入和输出绑定。You can create custom input and output bindings. 绑定必须以 .NET 编写,但可以在任何支持的语言中使用。Bindings must be authored in .NET, but can be consumed from any supported language. 有关创建自定义绑定的详细信息,请参阅创建自定义输入和输出绑定For more information about creating custom bindings, see Creating custom input and output bindings.


后续步骤Next steps