Dapr 狀態輸入繫結可讓您在函式執行期間讀取 Dapr 狀態。
如需 Dapr 延伸模組的安裝和設定詳細資料,請參閱 Dapr 延伸模組概觀。
Example
您可以使用下列其中一種 C# 模式來建立 C# 函式:
| Execution model | Description |
|---|---|
| 隔離式背景工作模型 | 您的函數程式碼在個別的 .NET 背景工作處理序中執行。 搭配支援的 .NET 和 .NET Framework 版本使用。 若要深入瞭解,請參閱 隔離背景工作模型中執行 C# Azure Functions 的指南。 |
| In-process model | 您的函數程式碼執行的處理序與 Functions 主機處理序相同。 僅支援長期支援 (LTS) 的 .NET 版本。 若要深入瞭解,請參閱 使用 Azure Functions 開發 C# 類別庫函式。 |
[FunctionName("StateInputBinding")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "state/{key}")] HttpRequest req,
[DaprState("statestore", Key = "{key}")] string state,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult(state);
}
下列範例會使用 "RetrieveOrder" 繫結搭配 DaprStateInput 來建立 DaprServiceInvocationTrigger 函式:
@FunctionName("RetrieveOrder")
public String run(
@DaprServiceInvocationTrigger(
methodName = "RetrieveOrder")
String payload,
@DaprStateInput(
stateStore = "%StateStoreName%",
key = "order")
String product,
final ExecutionContext context)
在下列範例中,Dapr 叫用輸入繫結會新增為 extraInput 並與 HTTP 觸發程序配對,該程式是由 app 物件註冊:
const { app, trigger } = require('@azure/functions');
app.generic('StateInputBinding', {
trigger: trigger.generic({
type: 'httpTrigger',
authLevel: 'anonymous',
methods: ['GET'],
route: "state/{key}",
name: "req"
}),
extraInputs: [daprStateInput],
handler: async (request, context) => {
context.log("Node HTTP trigger function processed a request.");
const daprStateInputValue = context.extraInputs.get(daprStateInput);
// print the fetched state value
context.log(daprStateInputValue);
return daprStateInputValue;
}
});
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 daprState:
{
"bindings":
{
"type": "daprState",
"direction": "in",
"key": "order",
"stateStore": "%StateStoreName%",
"name": "order"
}
}
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, $order
)
# C# function processed a CreateNewOrder request from the Dapr Runtime.
Write-Host "PowerShell function processed a RetrieveOrder request from the Dapr Runtime."
# Convert the object to a JSON-formatted string with ConvertTo-Json
$jsonString = $order | ConvertTo-Json
Write-Host "$jsonString"
下列範例示範使用 v2 Python 程式設計模型的 Dapr State 輸入繫結。 若要在 Python 函式應用程式程式碼中搭配 daprState 使用 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 DaprState to read Dapr state into your function, which supports these parameters:
| Parameter | Description |
|---|---|
| StateStore | 要擷取狀態的狀態存放區名稱。 |
| Key | 要從指定的狀態存放區擷取的金鑰名稱。 |
Annotations
DaprStateInput 註釋可讓您將 Dapr 狀態讀入您的函式。
| Element | Description |
|---|---|
| stateStore | Dapr 狀態存放區的名稱。 |
| key | 狀態存放區索引鍵值。 |
Configuration
下表說明您在程式碼中設定的繫結設定屬性。
| Property | Description |
|---|---|
| stateStore | 狀態存放區的名稱。 |
| key | 要從指定的狀態存放區擷取的金鑰名稱。 |
下表說明您在 function.json 檔案中設定的繫結設定屬性。
| function.json property | Description |
|---|---|
| key | 要從指定的狀態存放區擷取的金鑰名稱。 |
| stateStore | 狀態存放區的名稱。 |
See the Example section for complete examples.
Usage
若要使用 Dapr State 輸入繫結,請先設定 Dapr State 存放區元件。 若想深入了解要使用的元件及其設定方式,請參閱官方 Dapr 文件。