Azure Functions は、次の 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# クラス ライブラリ関数を開発する」を参照してください。 |
[FunctionName("CreateNewOrder")]
public static void Run(
[DaprServiceInvocationTrigger] JObject payload,
[DaprState("%StateStoreName%", Key = "order")] out JToken order,
ILogger log)
{
log.LogInformation("C# function processed a CreateNewOrder request from the Dapr Runtime.");
// payload must be of the format { "data": { "value": "some value" } }
order = payload["data"];
}
Dapr サービス呼び出しトリガーの Java コードを次に示します。
@FunctionName("CreateNewOrder")
public String run(
@DaprServiceInvocationTrigger(
methodName = "CreateNewOrder")
)
app オブジェクトを使用して、daprInvokeOutputを登録します。
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 daprServiceInvocationTrigger:
{
"bindings": [
{
"type": "daprServiceInvocationTrigger",
"name": "payload",
"direction": "in"
}
]
}
For more information about function.json file properties, see the Configuration section.
In code:
using namespace System
using namespace Microsoft.Azure.WebJobs
using namespace Microsoft.Extensions.Logging
using namespace Microsoft.Azure.WebJobs.Extensions.Dapr
using namespace Newtonsoft.Json.Linq
param (
$payload
)
# C# function processed a CreateNewOrder request from the Dapr Runtime.
Write-Host "PowerShell function processed a CreateNewOrder request from the Dapr Runtime."
# Payload must be of the format { "data": { "value": "some value" } }
# Convert the object to a JSON-formatted string with ConvertTo-Json
$jsonString = $payload| ConvertTo-Json
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name order -Value $payload["data"]
次の例は、 v2 Python プログラミング モデルを使用する Dapr サービス呼び出しトリガーを示しています。 Python 関数アプリ コードで daprServiceInvocationTrigger を使用するには:
import logging
import json
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="RetrieveOrder")
@app.dapr_service_invocation_trigger(arg_name="payload", method_name="RetrieveOrder")
@app.dapr_state_input(arg_name="data", state_store="statestore", key="order")
def main(payload, data: str) :
# Function should be invoked with this command: dapr invoke --app-id functionapp --method RetrieveOrder --data '{}'
logging.info('Python function processed a RetrieveOrder request from the Dapr Runtime.')
logging.info(data)
Attributes
In the in-process model, use the DaprServiceInvocationTrigger to trigger a Dapr service invocation binding, which supports the following properties.
| Parameter | Description |
|---|---|
| MethodName | Optional. Dapr 呼び出し元が使用するメソッドの名前。 指定しない場合は、関数の名前がメソッド名として使用されます。 |
Annotations
DaprServiceInvocationTrigger注釈を使用すると、Dapr ランタイムによって呼び出される関数を作成できます。
| Element | Description |
|---|---|
| methodName | メソッド名。 |
Configuration
次の表では、コードで設定するバインド構成プロパティについて説明します。
| Property | Description |
|---|---|
| type |
daprServiceInvocationTriggerに設定する必要があります。 |
| name | 関数コード内の Dapr データを表す変数の名前。 |
次の表は、function.json ファイルで設定したバインド構成のプロパティを説明しています。
| function.json property | Description |
|---|---|
| type |
daprServiceInvocationTriggerに設定する必要があります。 |
| name | 関数コード内の Dapr データを表す変数の名前。 |
See the Example section for complete examples.
Usage
Dapr サービス呼び出しトリガーを使用するには、サービス呼び出しトリガーで使用するコンポーネントと、それらを設定する方法の詳細については、Dapr の公式ドキュメントを参照してください。
Python v2 で daprServiceInvocationTrigger を使用するには、正しい依存関係でプロジェクトを設定します。
requirements.textファイルに、次の行を追加します。azure-functions==1.18.0b3ターミナルで、Python ライブラリをインストールします。
pip install -r .\requirements.txt次の構成で
local.setting.jsonファイルを変更します。"PYTHON_ISOLATE_WORKER_DEPENDENCIES":1