gpt4 book ai didi

c# - 如何将输出值绑定(bind)到我的异步 Azure Functions?

转载 作者:可可西里 更新时间:2023-11-01 07:56:03 25 4
gpt4 key购买 nike

如何将我的输出绑定(bind)到异步函数?将参数设置为 out 的常用方法不适用于异步函数。

示例

using System;

public static async void Run(string input, TraceWriter log, out string blobOutput)
{
log.Info($"C# manually triggered function called with input: {input}");
await Task.Delay(1);

blobOutput = input;
}

这会导致编译错误:

[timestamp] (3,72): error CS1988: Async methods cannot have ref or out parameters

使用的绑定(bind)(仅供引用)

{
"bindings": [
{
"type": "blob",
"name": "blobOutput",
"path": "testoutput/{rand-guid}.txt",
"connection": "AzureWebJobsDashboard",
"direction": "out"
},
{
"type": "manualTrigger",
"name": "input",
"direction": "in"
}
],
"disabled": false
}

最佳答案

有几种方法可以做到这一点:

将输出绑定(bind)到函数的返回值(最简单)

然后您可以简单地从函数返回值。您必须将输出绑定(bind)的名称设置为 $return 才能使用此方法

代码

public static async Task<string> Run(string input, TraceWriter log)
{
log.Info($"C# manually triggered function called with input: {input}");
await Task.Delay(1);

return input;
}

绑定(bind)

{
"bindings": [
{
"type": "blob",
"name": "$return",
"path": "testoutput/{rand-guid}.txt",
"connection": "AzureWebJobsDashboard",
"direction": "out"
},
{
"type": "manualTrigger",
"name": "input",
"direction": "in"
}
],
"disabled": false
}

将输出绑定(bind)到 IAsyncCollector

将输出绑定(bind)到 IAsyncCollector 并将您的项目添加到收集器。

当您有多个输出绑定(bind)时,您将需要使用此方法。

代码

public static async Task Run(string input, IAsyncCollector<string> collection, TraceWriter log)
{
log.Info($"C# manually triggered function called with input: {input}");
await collection.AddAsync(input);
}

绑定(bind)

{
"bindings": [
{
"type": "blob",
"name": "collection",
"path": "testoutput/{rand-guid}.txt",
"connection": "AzureWebJobsDashboard",
"direction": "out"
},
{
"type": "manualTrigger",
"name": "input",
"direction": "in"
}
],
"disabled": false
}

关于c# - 如何将输出值绑定(bind)到我的异步 Azure Functions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40409344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com