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. 计时器Timer Blob 存储Blob Storage Cosmos DBCosmos DB
一个 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 approach.

平台Platform 触发器和绑定的配置方式...Triggers and bindings are configured by...
C# 类库C# class library      使用 C# 特性修饰方法和参数     decorating methods and parameters with C# attributes
其他所有(包括 Azure 门户)All others (including Azure portal)      更新 function.json架构     updating function.json (schema)

门户为此配置提供了一个 UI,但你可以通过函数的“集成”选项卡打开“高级编辑器”,来直接编辑文件。The portal provides a UI for this configuration, but you can edit the file directly by opening the Advanced editor available via the Integrate tab of your function.

在 .NET 中,参数类型定义了输入数据的数据类型。In .NET, 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.

对于动态键入的语言(如 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.

支持的绑定Supported bindings

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

类型Type 1.x1.x 2.x12.x1 触发器Trigger 输入Input 输出Output
Blob 存储Blob storage
Cosmos DBCosmos DB
事件中心Event Hubs
HTTP 和 WebhookHTTP & webhooks
Microsoft Graph
Excel 表
Microsoft Graph
Excel tables
Microsoft Graph
OneDrive 文件
Microsoft Graph
OneDrive files
Microsoft Graph
Outlook 电子邮件
Microsoft Graph
Outlook email
Microsoft Graph
事件
Microsoft Graph
events
Microsoft Graph
身份验证令牌
Microsoft Graph
Auth tokens
移动应用Mobile Apps
通知中心Notification Hubs
队列存储Queue storage
SendGridSendGrid
服务总线Service Bus
表存储Table storage
计时器Timer

1 在 2.x 中,除了 HTTP 和 Timer 以外,所有绑定都必须注册。1 In 2.x, all bindings except HTTP and Timer must be registered. 请参阅注册绑定扩展See Register binding extensions.

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

资源Resources

后续步骤Next steps