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
使用 Microsoft Graph 來更新 Excel 工作表的 webhook。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)      更新函數. json架構     updating function.json (schema)

入口網站會提供此設定的 UI,但是您可以透過函式的 [整合] 索引標籤開啟可用的 [ Advanced editor ],直接編輯檔案。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 等具有動態類型的語言,則會使用 dataTypefunction.json檔案中的 屬性。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.jsondirection 檔案中都具有 屬性: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,則只能透過入口網站中的 [整合] 索引標籤使用 [ Advanced editor ]。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 major versions of the Azure Functions runtime:

TypeType 1.x1.x 2.x 和更新版本12.x and higher1 觸發程序Trigger 輸入Input 輸出Output
Blob 儲存體Blob storage
Cosmos DBCosmos DB
Event GridEvent Grid
事件中樞Event Hubs
HTTP & webhookHTTP & webhooks
IoT 中心IoT Hub
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
Microsoft Graph
Microsoft Graph
Auth tokens
行動應用程式Mobile Apps
通知中樞Notification Hubs
佇列儲存體Queue storage
服務匯流排Service Bus
資料表儲存體Table storage

1從版本2.x 執行時間開始,必須註冊 HTTP 和計時器以外的所有系結。1 Starting with the version 2.x runtime, 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.


後續步驟Next steps