Dapr 輸出繫結可讓您在函式執行期間,將值傳送至 Dapr 輸出繫結。
如需 Dapr 延伸模組的安裝和設定詳細資料,請參閱 Dapr 延伸模組概觀。
Example
您可以使用下列其中一種 C# 模式來建立 C# 函式:
| Execution model | Description |
|---|---|
| 隔離式背景工作模型 | 您的函數程式碼在個別的 .NET 背景工作處理序中執行。 搭配支援的 .NET 和 .NET Framework 版本使用。 若要深入瞭解,請參閱 隔離背景工作模型中執行 C# Azure Functions 的指南。 |
| In-process model | 您的函數程式碼執行的處理序與 Functions 主機處理序相同。 僅支援長期支援 (LTS) 的 .NET 版本。 若要深入瞭解,請參閱 使用 Azure Functions 開發 C# 類別庫函式。 |
下列範例示範如何使用 Dapr 服務叫用觸發程式和 Dapr 輸出繫結來讀取和處理繫結要求。
[FunctionName("SendMessageToKafka")]
public static async Task Run(
[DaprServiceInvocationTrigger] JObject payload,
[DaprBinding(BindingName = "%KafkaBindingName%", Operation = "create")] IAsyncCollector<object> messages,
ILogger log)
{
log.LogInformation("C# function processed a SendMessageToKafka request.");
await messages.AddAsync(payload);
}
下列範例會使用 "SendMessageToKafka" 繫結搭配 DaprBindingOutput 來建立 DaprServiceInvocationTrigger 函式:
@FunctionName("SendMessageToKafka")
public String run(
@DaprServiceInvocationTrigger(
methodName = "SendMessageToKafka")
String payload,
@DaprBindingOutput(
bindingName = "%KafkaBindingName%",
operation = "create")
OutputBinding<String> product,
final ExecutionContext context) {
context.getLogger().info("Java function processed a SendMessageToKafka request.");
product.setValue(payload);
return payload;
}
在下列範例中,Dapr 輸出繫結會與 Dapr 叫用輸出觸發程式配對,該程式是由 app 物件所註冊:
const { app, trigger } = require('@azure/functions');
app.generic('SendMessageToKafka', {
trigger: trigger.generic({
type: 'daprServiceInvocationTrigger',
name: "payload"
}),
return: daprBindingOutput,
handler: async (request, context) => {
context.log("Node function processed a SendMessageToKafka request from the Dapr Runtime.");
context.log(context.triggerMetadata.payload)
return { "data": context.triggerMetadata.payload };
}
});
The following examples show Dapr triggers in a function.json file and PowerShell code that uses those bindings.
Here's the function.json file for daprBinding:
{
"bindings":
{
"type": "daprBinding",
"direction": "out",
"bindingName": "%KafkaBindingName%",
"operation": "create",
"name": "messages"
}
}
For more information about function.json file properties, see the Configuration section.
In code:
using namespace System.Net
# Input bindings are passed in via param block.
param($req, $TriggerMetadata)
Write-Host "Powershell SendMessageToKafka processed a request."
$invoke_output_binding_req_body = @{
"data" = $req
}
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name messages -Value $invoke_output_binding_req_body
下列範例示範使用 v2 Python 程式設計模型的 Dapr Binding 輸出繫結。 若要在您的 Python 函式應用程式程式碼中使用 @dapp.dapr_binding_output :
import logging
import json
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="SendMessageToKafka")
@app.dapr_service_invocation_trigger(arg_name="payload", method_name="SendMessageToKafka")
@app.dapr_binding_output(arg_name="messages", binding_name="%KafkaBindingName%", operation="create")
def main(payload: str, messages: func.Out[bytes]) -> None:
logging.info('Python processed a SendMessageToKafka request from the Dapr Runtime.')
messages.set(json.dumps({"data": payload}).encode('utf-8'))
Attributes
In the in-process model, use the DaprBinding to define a Dapr binding output binding, which supports these parameters:
| Parameter | Description | 可透過屬性傳送 | 可透過 RequestBody 傳送 |
|---|---|---|---|
| BindingName | Dapr 繫結的名稱。 | ✔️ | ✔️ |
| Operation | 已設定的繫結作業。 | ✔️ | ✔️ |
| Metadata | 中繼資料命名空間。 | ❌ | ✔️ |
| Data | Required. 繫結作業的資料。 | ❌ | ✔️ |
Annotations
DaprBindingOutput 註釋可讓您建立傳送輸出繫結的函式。
| Element | Description | 可透過屬性傳送 | 可透過 RequestBody 傳送 |
|---|---|---|---|
| bindingName | Dapr 繫結的名稱。 | ✔️ | ✔️ |
| output | 已設定的繫結作業。 | ✔️ | ✔️ |
| metadata | 中繼資料命名空間。 | ❌ | ✔️ |
| data | Required. 繫結作業的資料。 | ❌ | ✔️ |
Configuration
下表說明您在程式碼中設定的繫結設定屬性。
| Property | Description | 可透過屬性傳送 | 可透過 RequestBody 傳送 |
|---|---|---|---|
| bindingName | 繫結的名稱。 | ✔️ | ✔️ |
| operation | 繫結作業。 | ✔️ | ✔️ |
| metadata | 中繼資料命名空間。 | ❌ | ✔️ |
| data | Required. 繫結作業的資料。 | ❌ | ✔️ |
下表說明您在 function.json 檔案中設定的繫結設定屬性。
| function.json property | Description | 可透過屬性傳送 | 可透過 RequestBody 傳送 |
|---|---|---|---|
| bindingName | 繫結的名稱。 | ✔️ | ✔️ |
| operation | 繫結作業。 | ✔️ | ✔️ |
| metadata | 中繼資料命名空間。 | ❌ | ✔️ |
| data | Required. 繫結作業的資料。 | ❌ | ✔️ |
如果屬性在 Attributes 和 RequestBody 中都有定義,則系統會優先考量 RequestBody 中提供的資料。
See the Example section for complete examples.
Usage
若要使用 Dapr 輸出繫結,請先設定 Dapr 輸出繫結元件。 您可以在官方 Dapr 文件中深入了解要使用哪個元件,以及如何進行設定。