前言
原文
What is trigger and binding
「Trigger」定義了 function 會怎樣及在何時參與到整個工作流程
每個 function 都會有且只有一個 trigger
Trigger 通常都會附帶一些相關資料並作為 function 的 payload (承載)
「Binding」定義了 function 會怎樣連接到其他資源(resources)
Binding 共分為 input binding 和 output binding 兩種
對於 function 來說,binding 是 optional 的
而且一個 function 也可以有一個或多個不同的 binding
來自 binding 的數據會成為 function 的 parameter
Trigger and binding definitions
不同的 deployment language 會有着不同的定義 trigger 和 binding 的方式
Language | 設置 trigger 和 binding 的方式 |
---|---|
C# class library | decorating methods and parameters with C# attributes |
Java | decorating methods and parameters with Java annotations |
JavaScript/PowerShell/Python/TypeScript | updating function.json schema |
如果你用的是使用 function.json
的 language
你可以在 Azure Portal 的 “Integration tab” 設置 binding
也可以在 “Code + test tab” 修改你的 function 的 檔案
如果你的是 .Net
或 Java
Parameter type 決定了 input data type
例如你可以用string
綁定來自 queue trigger 的字串
你可以 byte array 去讀取 binary
如果用 azure portal 修改 C# script
那就會是使用 function.json 而不是 attributes
((上表不是說 C# 是用 attribute 的嗎,難道C#有兩種處理方式?
在動態類型(dynamically typed)的語言中,如 JavaScript,綁定的數據類型需要在 function.json
文件中指定。這是因為這些語言不會在編譯時確定變量的類型,因此需要明確告訴 Azure Functions 如何處理輸入數據。
例如,當你想以二進制格式讀取 HTTP 請求的內容時,可以在 function.json
文件中設置 dataType
屬性為 binary
。這樣,Azure Functions 就能夠正確解析和處理該請求的內容。下面是一個設置的例子。
{ "dataType": "binary", "type": "httpTrigger", "name": "req", "direction": "in" }
dataType
還可以是stream
和 string
Binding direction
在function.json
中設置的 trigger 和 binding 都需要有 direction
- trigger 的
direction
只能是in
- input binding 的
direction
是in
- output binding 的
direction
是out
- 某些 binding 的 direction 可以設置為
inout
只有 Azure Portal 的 “Integration tab” 可以設置inout
當你使用 class library 來設置 trigger 和 binding
你可以使用 “attribute constructor” 來設置 direction
或者 Azure 會根據 parameter type 來判斷 direction
Azure Functions trigger and binding example
為了能更好的了解 trigger 和 binding
接下來我們就一起來看看下面這個例子吧
這個例子的情景是
每當 Azure Queue storage 有新 message 的時候
就在 Azure Table storage 寫入一個新的 row
這可以經由設置 Azure Queue storage trigger 和 Azure Table storage binding 完成
function.json
{ "disabled": false, "bindings": [ { "type": "queueTrigger", "direction": "in", "name": "myQueueItem", "queueName": "myqueue-items", "connection":"MyStorageConnectionAppSetting" }, { "tableName": "Person", "connection": "MyStorageConnectionAppSetting", "name": "tableBinding", "type": "table", "direction": "out" } ] }
在這個例子中bindings
這個 array 設置了所需的 trigger 和 binding
這個 array 的第一個 element 就設置了 triggertype
和 direction
就把它定義為 triggername
定義了你的 function 可以通過那一個 parameter 來取得 queue messagequeueName
定義了要監察那一個 queueconnection
則是放置了 connection string
這個 array 的第二個 element 就設置了 output bindingtype
和 direction
就把它定義為 output bindingname
定義了你的 function 會如何提供數據
在這個例子中就是把 function return value 加插為 new table rowtableName
指明了要把 value 輸出到那個 tableconnection
是儲存了連接到該 table 的 connection string
C# function example
C# 和 Java 都不是用 function.json
來定義 trigger 和 binding 的
而是用程式碼來定義
下面就是一個 C# 用 attribute 來定義 trigger 和 binding 的例子
(queue and table names, storage accounts, and function parameters for input and output)
public static class QueueTriggerTableOutput { [FunctionName("QueueTriggerTableOutput")] [return: Table("outTable", Connection = "MY_TABLE_STORAGE_ACCT_APP_SETTING")] public static Person Run( [QueueTrigger("myqueue-items", Connection = "MY_STORAGE_ACCT_APP_SETTING")]JObject order, ILogger log) { return new Person() { PartitionKey = "Orders", RowKey = Guid.NewGuid().ToString(), Name = order["Name"].ToString(), MobileNumber = order["MobileNumber"].ToString() }; } } public class Person { public string PartitionKey { get; set; } public string RowKey { get; set; } public string Name { get; set; } public string MobileNumber { get; set; } }
發佈留言