Dapr 叫用輸出系結可讓您在函式執行期間叫用另一個 Dapr 應用程式。
如需 Dapr 延伸模組的安裝和設定詳細資料,請參閱 Dapr 延伸模組概觀。
Example
您可以使用下列其中一種 C# 模式來建立 C# 函式:
| Execution model | Description |
|---|---|
| 隔離式工作者模型 | 您的函數程式碼在個別的 .NET 背景工作處理序中執行。 搭配 支援的 .NET 和 .NET Framework 版本使用。 若要深入瞭解,請參閱 隔離背景工作模型中執行 C# Azure Functions 的指南。 |
| In-process model | 您的函數程式碼執行的處理序與 Functions 主機處理序相同。 僅支援 .NET 的長期支援 (LTS) 版本。 若要深入瞭解,請參閱 使用 Azure Functions 開發 C# 類別庫函式。 |
下列範例示範如何使用 Dapr 叫用輸出系結來執行裝載於另一個 Dapr 化應用程式中的 Dapr 服務調用作業。 在此範例中,函式的作用就像 Proxy。
[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
})
下列範例顯示 Dapr Invoke 輸出系結,其使用 v2 Python 程式設計模型。 若要在您的 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 | 可透過屬性傳送 | 可透過 RequestBody 傳送 |
|---|---|---|---|
| AppId | 要叫用的 Dapr 應用程式識別碼。 | ✔️ | ✔️ |
| MethodName | 要叫用之應用程式的方法名稱。 | ✔️ | ✔️ |
| HttpVerb | Optional. 使用應用程式叫用的 HTTP 動詞。 預設值為 POST。 |
✔️ | ✔️ |
| Body | Required. 要求的本文。 | ❌ | ✔️ |
Annotations
批 DaprInvokeOutput 注可讓您讓函式叫用並接聽輸出系結。
| Element | Description | 可透過屬性傳送 | 可透過 RequestBody 傳送 |
|---|---|---|---|
| appId | 叫用系結中涉及之應用程式的應用程式標識碼。 | ✔️ | ✔️ |
| methodName | 方法變數的名稱。 | ✔️ | ✔️ |
| httpVerb | 張貼或取得。 | ✔️ | ✔️ |
| body | Required. 要求的本文。 | ❌ | ✔️ |
Configuration
下表說明您在程式碼中設定的繫結設定屬性。
| Property | Description | 可透過屬性傳送 | 可透過 RequestBody 傳送 |
|---|---|---|---|
| appId | 叫用系結中涉及之應用程式的應用程式標識碼。 | ✔️ | ✔️ |
| methods | 張貼或取得。 | ✔️ | ✔️ |
| body | Required. 要求的本文。 | ❌ | ✔️ |
下表說明您在 function.json 檔案中設定的繫結設定屬性。
| function.json property | Description | 可透過屬性傳送 | 可透過 RequestBody 傳送 |
|---|---|---|---|
| appId | 叫用系結中涉及之應用程式的應用程式標識碼。 | ✔️ | ✔️ |
| methodName | 方法變數的名稱。 | ✔️ | ✔️ |
| httpVerb | 張貼或取得。 | ✔️ | ✔️ |
| body | Required. 要求的本文。 | ❌ | ✔️ |
如果屬性在 Attributes 和 RequestBody 中都有定義,則系統會優先考量 RequestBody 中提供的資料。
See the Example section for complete examples.
Usage
若要使用 Dapr 服務調用輸出系結,請深入瞭解 如何在官方 Dapr 檔中使用 Dapr 服務調用。