Dapr 出力バインドを使用すると、関数の実行中に値を Dapr 出力バインドに送信できます。
Dapr 拡張機能のセットアップと構成について詳しくは、Dapr 拡張機能の概要に関する記事をご覧ください。
Example
A C# 関数は、次の C# モードのいずれかを使用して作成できます。
| Execution model | Description |
|---|---|
| 分離ワーカー モデル | 関数コードは、別の .NET ワーカー プロセスで実行されます。 .NET と .NET Framework のサポートされているバージョンで使います。 詳細については、 分離ワーカー モデルで C# Azure Functions を実行するためのガイドを参照してください。 |
| In-process model | 関数コードは、Functions ホスト プロセスと同じプロセスで実行されます。 .NET の長期サポート (LTS) バージョンのみをサポートします。 詳細については、「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 出力バインドは、app オブジェクトによって登録される Dapr 呼び出し出力トリガーとペアになっています。
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 | Attribute を使って送信できる | RequestBody を使って送信できる |
|---|---|---|---|
| BindingName | Dapr バインドの名前。 | ✔️ | ✔️ |
| Operation | 構成されたバインド操作。 | ✔️ | ✔️ |
| Metadata | メタデータ名前空間。 | ❌ | ✔️ |
| Data | Required. バインド操作のデータ。 | ❌ | ✔️ |
Annotations
注釈 DaprBindingOutput を使用すると、出力バインドを送信する関数を作成できます。
| Element | Description | Attribute を使って送信できる | RequestBody を使って送信できる |
|---|---|---|---|
| bindingName | Dapr バインドの名前。 | ✔️ | ✔️ |
| output | 構成されたバインド操作。 | ✔️ | ✔️ |
| metadata | メタデータ名前空間。 | ❌ | ✔️ |
| data | Required. バインド操作のデータ。 | ❌ | ✔️ |
Configuration
次の表では、コードで設定するバインド構成プロパティについて説明します。
| Property | Description | Attribute を使って送信できる | RequestBody を使って送信できる |
|---|---|---|---|
| bindingName | バインディングの名前。 | ✔️ | ✔️ |
| operation | バインド操作。 | ✔️ | ✔️ |
| metadata | メタデータ名前空間。 | ❌ | ✔️ |
| data | Required. バインド操作のデータ。 | ❌ | ✔️ |
次の表は、function.json ファイルで設定したバインド構成のプロパティを説明しています。
| function.json property | Description | Attribute を使って送信できる | RequestBody を使って送信できる |
|---|---|---|---|
| bindingName | バインディングの名前。 | ✔️ | ✔️ |
| operation | バインド操作。 | ✔️ | ✔️ |
| metadata | メタデータ名前空間。 | ❌ | ✔️ |
| data | Required. バインド操作のデータ。 | ❌ | ✔️ |
プロパティが Attributes と RequestBody の両方で定義されている場合は、RequestBody で指定されているデータが優先されます。
See the Example section for complete examples.
Usage
Dapr 出力バインドを使用するには、最初に Dapr 出力バインド コンポーネントを設定します。 使用するコンポーネントとその設定方法の詳細については、Dapr の公式ドキュメントを参照してください。
Python v2 で daprBinding を使用するには、正しい依存関係でプロジェクトを設定します。
requirements.textファイルに、次の行を追加します。azure-functions==1.18.0b3ターミナルで、Python ライブラリをインストールします。
pip install -r .\requirements.txt次の構成で
local.setting.jsonファイルを変更します。"PYTHON_ISOLATE_WORKER_DEPENDENCIES":1