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
Event Grid 用來讀取 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
若要更新的 Excel 工作表使用 Microsoft Graph 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)      updating function.json (schema)     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 執行時間的兩個主要版本中支援的系結:This table shows the bindings that are supported in the two major versions of the Azure Functions runtime:

TypeType 1.x1.x 2.x12.x1 觸發程序Trigger 輸入Input OutputOutput
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
Microsoft Graph
Microsoft Graph
Auth tokens
行動應用程式Mobile Apps
通知中樞Notification Hubs
佇列儲存體Queue storage
服務匯流排Service Bus
資料表儲存體Table storage

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.


後續步驟Next steps