您可以使用下列 Dapr 事件,在 Dapr 服務調用上觸發 Azure Functions。
如需 Dapr 延伸模組的安裝和設定詳細資料,請參閱 Dapr 延伸模組概觀。
Example
您可以使用下列其中一種 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 服務調用觸發程式。
daprServiceInvocationTrigger若要在 Python 函式應用程式程式代碼中使用 :
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 檔中設定它們。