您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

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 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.

将绑定添加到函数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
SendGridSendGrid 输出Output
服务总线Service Bus 触发器Trigger
SignalRSignalR 触发器Trigger
表存储Table storage 输入Input
TimerTimer 触发器Trigger 链接Link
TwilioTwilio 输出Output 链接Link


后续步骤Next steps