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 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)      更新 (架構) 上的function.js     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 等具有動態類型的語言,則會使用 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 ,則只有 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.

將系結新增至函式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
Dapr3Dapr3
Event GridEvent Grid
事件中樞Event Hubs
HTTP 和 WebhookHTTP & webhooks
IoT 中心IoT Hub
Kafka2Kafka2
Mobile AppsMobile Apps
通知中樞Notification Hubs
佇列儲存體Queue storage
RabbitMQ2RabbitMQ2
SendGridSendGrid
服務匯流排Service Bus
SignalRSignalR
表格儲存體Table storage
計時器Timer
TwilioTwilio

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.

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
輸入Input
輸出Output
連結Link
Azure Cosmos DBAzure Cosmos DB 觸發程序Trigger
輸入Input
輸出Output
連結Link
事件方格Event Grid 觸發程序Trigger
輸出Output
連結Link
事件中樞Event Hubs 觸發程序Trigger
輸出Output
IoT 中樞IoT Hub 觸發程序Trigger
輸出Output
HTTPHTTP 觸發程序Trigger 連結Link
佇列儲存體Queue storage 觸發程序Trigger
輸出Output
連結Link
SendGridSendGrid 輸出Output
服務匯流排Service Bus 觸發程序Trigger
輸出Output
連結Link
SignalRSignalR 觸發程序Trigger
輸入Input
輸出Output
表格儲存體Table storage 輸入Input
輸出Output
計時器Timer 觸發程序Trigger 連結Link
TwilioTwilio 輸出Output 連結Link

資源Resources

後續步驟Next steps