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 でホストされている Dapr サービス呼び出し操作を実行する方法を示します。 この例では、関数はプロキシのように機能します。
[FunctionName("InvokeOutputBinding")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "invoke/{appId}/{methodName}")] HttpRequest req,
[DaprInvoke(AppId = "{appId}", MethodName = "{methodName}", HttpVerb = "post")] IAsyncCollector<InvokeMethodParameters> output,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var outputContent = new InvokeMethodParameters
{
Body = requestBody
};
await output.AddAsync(outputContent);
return new OkResult();
}
次の例では、"InvokeOutputBinding" バインディングとDaprInvokeOutputを使用してHttpTrigger関数を作成します。
@FunctionName("InvokeOutputBinding")
public String run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS,
route = "invoke/{appId}/{methodName}")
HttpRequestMessage<Optional<String>> request,
@DaprInvokeOutput(
appId = "{appId}",
methodName = "{methodName}",
httpVerb = "post")
OutputBinding<String> payload,
final ExecutionContext context)
次の例では、Dapr 呼び出し出力バインドは、 app オブジェクトによって登録される HTTP トリガーとペアになっています。
const { app, trigger } = require('@azure/functions');
app.generic('InvokeOutputBinding', {
trigger: trigger.generic({
type: 'httpTrigger',
authLevel: 'anonymous',
methods: ['POST'],
route: "invoke/{appId}/{methodName}",
name: "req"
}),
return: daprInvokeOutput,
handler: async (request, context) => {
context.log("Node HTTP trigger function processed a request.");
const payload = await request.text();
context.log(JSON.stringify(payload));
return { body: 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 daprInvoke:
{
"bindings":
{
"type": "daprInvoke",
"direction": "out",
"appId": "{appId}",
"methodName": "{methodName}",
"httpVerb": "post",
"name": "payload"
}
}
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 to the Azure Functions log stream.
Write-Host "Powershell InvokeOutputBinding processed a request."
$req_body = $req.Body
$invoke_output_binding_req_body = @{
"body" = $req_body
}
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name payload -Value $invoke_output_binding_req_body
Push-OutputBinding -Name res -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $req_body
})
次の例は、 v2 Python プログラミング モデルを使用する Dapr Invoke 出力バインドを示しています。 Python 関数アプリ コードで daprInvoke を使用するには:
import logging
import json
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="InvokeOutputBinding")
@app.route(route="invoke/{appId}/{methodName}", auth_level=dapp.auth_level.ANONYMOUS)
@app.dapr_invoke_output(arg_name = "payload", app_id = "{appId}", method_name = "{methodName}", http_verb = "post")
def main(req: func.HttpRequest, payload: func.Out[str] ) -> str:
# request body must be passed this way "{\"body\":{\"value\":{\"key\":\"some value\"}}}" to use the InvokeOutputBinding, all the data must be enclosed in body property.
logging.info('Python function processed a InvokeOutputBinding request from the Dapr Runtime.')
body = req.get_body()
logging.info(body)
if body is not None:
payload.set(body)
else:
logging.info('req body is none')
return 'ok'
Attributes
In the in-process model, use the DaprInvoke attribute to define a Dapr invoke output binding, which supports these parameters:
| Parameter | Description | Attribute を使って送信できる | RequestBody を使って送信できる |
|---|---|---|---|
| AppId | 呼び出す Dapr アプリ ID。 | ✔️ | ✔️ |
| MethodName | 呼び出すアプリのメソッド名。 | ✔️ | ✔️ |
| HttpVerb | Optional. 呼び出すアプリを使用する HTTP 動詞。 既定値は POST です。 |
✔️ | ✔️ |
| Body | Required. 要求の本文。 | ❌ | ✔️ |
Annotations
DaprInvokeOutput注釈を使用すると、関数を呼び出して出力バインドをリッスンできます。
| Element | Description | Attribute を使って送信できる | RequestBody を使って送信できる |
|---|---|---|---|
| appId | 呼び出しバインドに関係するアプリケーションのアプリ ID。 | ✔️ | ✔️ |
| methodName | メソッド変数の名前。 | ✔️ | ✔️ |
| httpVerb | 投稿または取得。 | ✔️ | ✔️ |
| body | Required. 要求の本文。 | ❌ | ✔️ |
Configuration
次の表では、コードで設定するバインド構成プロパティについて説明します。
| Property | Description | Attribute を使って送信できる | RequestBody を使って送信できる |
|---|---|---|---|
| appId | 呼び出しバインドに関係するアプリケーションのアプリ ID。 | ✔️ | ✔️ |
| methods | 投稿または取得。 | ✔️ | ✔️ |
| body | Required. 要求の本文。 | ❌ | ✔️ |
次の表は、function.json ファイルで設定したバインド構成のプロパティを説明しています。
| function.json property | Description | Attribute を使って送信できる | RequestBody を使って送信できる |
|---|---|---|---|
| appId | 呼び出しバインドに関係するアプリケーションのアプリ ID。 | ✔️ | ✔️ |
| methodName | メソッド変数の名前。 | ✔️ | ✔️ |
| httpVerb | 投稿または取得。 | ✔️ | ✔️ |
| body | Required. 要求の本文。 | ❌ | ✔️ |
プロパティが Attributes と RequestBody の両方で定義されている場合は、RequestBody で指定されているデータが優先されます。
See the Example section for complete examples.
Usage
Dapr サービス呼び出し出力バインドを使用するには、 Dapr サービス呼び出しを使用する方法の詳細については、Dapr の公式ドキュメントを参照してください。
Python v2 で daprInvoke を使用するには、正しい依存関係でプロジェクトを設定します。
requirements.textファイルに、次の行を追加します。azure-functions==1.18.0b3ターミナルで、Python ライブラリをインストールします。
pip install -r .\requirements.txt次の構成で
local.setting.jsonファイルを変更します。"PYTHON_ISOLATE_WORKER_DEPENDENCIES":1