Azure Functions 的 Dapr 服務調用觸發程式

Warning

Azure Functions 的 Dapr 擴充功能將於 2027 年 4 月 20 日退休,並預定當天從擴充套件中移除。 此移除適用於主擴充包 4.42.0 及以上版本,以及預覽擴充包 4.60.0 及以上版本。

此移除同樣適用於那些預裝或快取於 Linux 主機映像檔,或透過 Windows 主機發佈的捆綁版本。

直接從函式程式碼遷移到 Dapr 用戶端 SDK 或 Dapr HTTP API ,並用能接收 Dapr 回調的 HTTP 觸發函式替換 Dapr 觸發器。 現有應用程式可以暫時將主套件釘選到 [4.*, 4.42.0) ,或將預覽套件固定在 [4.*, 4.60.0)host.json。 .NET 應用程式可以直接使用對應的 Dapr 擴充套件 NuGet 套件。 無論是固定舊的套件還是繼續使用 NuGet 套件,都不會延長支援超過 2027 年 4 月 20 日,也不會改變移除日期。

您可以使用下列 Dapr 事件,在 Dapr 服務調用上觸發 Azure Functions。

如需 Dapr 延伸模組的安裝和設定詳細資料,請參閱 Dapr 延伸模組概觀。

Example

您可以使用下列其中一種 C# 模式來建立 C# 函式:

執行模型 Description
隔離式工作者模型 您的函數程式碼在個別的 .NET 背景工作處理序中執行。 搭配 支援的 .NET 和 .NET Framework 版本使用。 若要深入瞭解,請參閱 隔離背景工作模型中執行 C# Azure Functions 的指南。
進程內模型 您的函數程式碼執行的處理序與 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 };
    }
});

以下範例展示了 function.json 檔案中的 Dapr 觸發器以及使用這些綁定的 PowerShell 程式碼。

以下是 的 daprServiceInvocationTrigger 檔案:

{
  "bindings": [
    {
      "type": "daprServiceInvocationTrigger",
      "name": "payload",
      "direction": "in"
    }
  ]
}

如需 function.json 檔案屬性的詳細資訊,請參閱設定一節。

在程式代碼中:

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

在 進行中模型中,使用來 DaprServiceInvocationTrigger 觸發 DAPR 服務調用綁定,該綁定支援以下特性。

Parameter Description
MethodName Optional. Dapr 呼叫端應該使用的方法名稱。 如果未指定,則會使用函式的名稱做為方法名稱。

Annotations

批 DaprServiceInvocationTrigger 注可讓您建立 Dapr 執行時間叫用的函式。

Element Description
methodName 方法名稱。

Configuration

下表說明您在程式碼中設定的繫結設定屬性。

Property Description
type 必須設定為 daprServiceInvocationTrigger。
name 代表函式程式代碼中 Dapr 數據的變數名稱。

下表說明您在 function.json 檔案中設定的繫結設定屬性。

function.json 屬性 Description
type 必須設定為 daprServiceInvocationTrigger。
name 代表函式程式代碼中 Dapr 數據的變數名稱。

下表說明您在 Python 程式碼中設定的 @dapp.dapr_service_invocation_trigger 繫結組態屬性。

Property Description
method_name 代表 Dapr 資料的變數名稱。

如需完整範例,請參閱範例一節。

Usage

若要使用 Dapr 服務調用觸發程式,請深入瞭解要與服務調用觸發程式搭配使用的元件,以及如何在官方 Dapr 檔中設定它們。

若要在 Python v2 中使用 daprServiceInvocationTrigger,請設定您的專案使其具備正確的相依性。

  1. 建立並啟用虛擬環境。

  2. 在 requirements.text 檔案中新增以下這一行:

    azure-functions==1.18.0b3
    
  3. 在終端機中,安裝 Python 程式庫。

    pip install -r .\requirements.txt
    
  4. 使用下列組態修改您的 local.setting.json 檔案:

    "PYTHON_ISOLATE_WORKER_DEPENDENCIES":1
    

下一步

深入了解 Dapr 服務叫用。