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* NoneNone 队列*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
事件网格用于读取 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 GridEvent 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 NoneNone 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 也另具有 streamstring 兩種選項。Other 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 執行階段中所支援的繫結。The following 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 GridEvent Grid
事件中樞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
SignalRSignalR
資料表儲存體Table storage
計時器Timer
TwilioTwilio

1 在 2.x 中,必須註冊 HTTP 和計時器以外的所有繫結。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