【问题标题】:How can I bind output values to my async Azure Function?如何将输出值绑定到我的异步 Azure 函数?
【发布时间】:2016-11-03 19:03:09
【问题描述】:

如何将我的输出绑定到异步函数?将参数设置为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

使用的绑定(仅供参考)

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

【问题讨论】:

    标签: c# azure asynchronous azure-functions


    【解决方案1】:

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

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

    然后你可以简单地从你的函数中返回值。您必须将输出绑定的名称设置为 $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;
    }
    

    绑定

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

    将输出绑定到 IAsyncCollector

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

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

    代码

    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);
    }
    

    绑定

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

    【讨论】:

    • 我在使用时遇到问题:ICollectorAsyncerror CS0246: The type or namespace name 'ICollectorAsync&lt;&gt;' could not be found
    • @Jean-philippeEmond 抱歉,应该是 IAsyncCollector
    • Blob 输出绑定不支持收集器,请参阅issue。另一种可能性请参见this answer
    【解决方案2】:

    我还没有能力发表评论,但在上面 Zain Rizvi 的代码中,应该说 IAsyncCollector:

    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);
    }
    

    【讨论】:

    • Blob 不支持 iasyncollector
    【解决方案3】:

    异步方法可以正常返回值,但是不应该返回纯类型的值,使用Task代替,像这样:

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

    【讨论】:

    • 噢!是的,我本来打算将返回值设置为 Task。我最初的答案概率中的功能甚至不起作用。谢谢,我已经修好了
    • 不要忘记,这还需要您将 function.json 中的“name”属性设置为“$return”以进行输出绑定。这告诉运行时使用函数的输出作为源。
    猜你喜欢
    • 2020-09-02
    • 2019-06-19
    • 2018-05-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2020-05-30
    相关资源
    最近更新 更多